Re: GtkTreeView : Drag and drop




Hi!

On Mon, 22 Aug 2005 00:37:34 +0200
Thym <thymus gmail com> wrote:

Hello,

( I speak french, sorry for my english )

in my GtkTreeView, I activated drag and drop with : 
gtk_tree_view_set_reorderable(GTK_TREE_VIEW(pTreeView), TRUE);

The DnD works fine, but I would like control if the row "may" moved.

How can I do it ?

here is some sample code from my app, but this is for a listview, not a
treeview,

typedef struct {
  GtkTreeModel *model;
  GtkTreePath *path;
  int index;
} drag_mrl_t;

static drag_mrl_t drag_mrl = { NULL, NULL, -1 };

 static void _treeview_drag_drop_setup (GtkTreeView *widget)
{
  GObject *object = G_OBJECT(widget);

//  printf ("treeview drag drop setup\n");
  g_signal_connect (object, "drag-begin",
                    G_CALLBACK (_drag_start_cb), NULL);
  g_signal_connect (object, "drag-data-get",
                    G_CALLBACK (_drag_data_get_cb), NULL);
  g_signal_connect (object, "drag-data-received",
                    G_CALLBACK (_drop_cb), NULL);
  g_signal_connect (object, "drag-end",
                    G_CALLBACK (_drag_end_cb), NULL);

  gtk_tree_view_enable_model_drag_source (widget, GDK_BUTTON1_MASK,
                                          target_table, 1,
                                          GDK_ACTION_COPY |
GDK_ACTION_MOVE); gtk_tree_view_enable_model_drag_dest (widget,
target_table, 1, GDK_ACTION_COPY | GDK_ACTION_MOVE);
}

static void _drag_end_cb (GtkTreeView *widget, GdkDragContext *context,
                         gpointer data)
{
//  printf ("drag end cb\n");
  if (drag_mrl.model)
  {
    drag_mrl.model = NULL;
    drag_mrl.index = -1;
    gtk_tree_path_free (drag_mrl.path);
  }
}
static void _drop_cb (GtkTreeView *widget,
                      GdkDragContext     *context,
                      gint                x,
                      gint                y,
                      GtkSelectionData   *data,
                      guint               info2,
                      guint time)
{
  gint ins_pos = treeview_get_drop_index (widget, context, x, y);
  GtkTreeIter iter;
  gint id, typ, katalog;
  gchar *nazwa, *path, *table, *q;
  gint ret;

//  printf ("drag_drop: drop callback, length=%d, format=%d, pos=%d,%d,
insert=%d\n", //             data->length, data->format, x, y, ins_pos);
  if (ins_pos == drag_mrl.index)
    return;
  path = g_strdup_printf ("%d", ins_pos);
  if (gtk_tree_model_get_iter_from_string (smodel, &iter, path))
  {
  }
  g_free (path);

  gtk_drag_finish (context, FALSE, FALSE, time);
}
static int treeview_get_drop_index (GtkTreeView *widget,
                                    GdkDragContext *context,
                                    gint x, gint y)
{
  GtkTreePath *path;
  gint cx, cy, ins_pos = -1;

  gdk_window_get_geometry (gtk_tree_view_get_bin_window (widget),
                           &cx, &cy, NULL, NULL, NULL);
  if (gtk_tree_view_get_path_at_pos (widget, x -= cx, y -= cy,
                                     &path, NULL, &cx, &cy))
  {
    GdkRectangle rect;
    /* in lower 1/3 of row? use next row as target */
    gtk_tree_view_get_background_area (widget, path,
                                       gtk_tree_view_get_column
(widget, 0), &rect);
    if (cy >= rect.height * 2 / 3.0)
    {
      gtk_tree_path_free (path);
      if (gtk_tree_view_get_path_at_pos (widget, x, y + rect.height,
                                         &path, NULL, &cx, &cy))
        ins_pos = gtk_tree_path_get_indices (path)[0];
    }
    else
      ins_pos = gtk_tree_path_get_indices (path)[0];
    gtk_tree_path_free (path);
  }
  return ins_pos;
}
static void _drag_data_get_cb (GtkTreeView *widget, GdkDragContext
*context, GtkSelectionData *selection, guint info,
                               guint time, gpointer data)
{
//  printf ("drag drop get cb\n");
  if (drag_mrl.model)
    gtk_tree_set_row_drag_data (selection, drag_mrl.model,
drag_mrl.path); }

static void _drag_start_cb (GtkTreeView *widget, GdkDragContext
*context, gpointer data)
{
  GtkTreeSelection *sel = gtk_tree_view_get_selection (widget);
  GtkTreeIter iter;

//  printf ("drag drop start\n");
  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
  {
    drag_mrl.model = gtk_tree_view_get_model (widget);
    drag_mrl.path = gtk_tree_model_get_path (drag_mrl.model, &iter);
    drag_mrl.index = gtk_tree_path_get_indices (drag_mrl.path)[0];
  }
}


-- 
HuamiSoft Hubert Sokolowski
http://www.huamisoft.com/
tel. (085) 7465779
kom. +48501456743



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]