+
+/*****************************************************************************/
+/* DRAG and DROP stuff */
+/*****************************************************************************/
+
+/*
+ * This function fills the #GtkSelectionData with the row and the
+ * model that has been dragged. It's called when this widget is a
+ * source for dnd after the event drop happened
+ */
+static void
+on_drag_data_get (GtkWidget *widget,
+ GdkDragContext *context,
+ GtkSelectionData *selection_data,
+ guint info,
+ guint time,
+ gpointer data)
+{
+ GtkTreeSelection *selection;
+ GtkTreeModel *model_sort, *model;
+ GtkTreeIter iter;
+ GtkTreePath *source_row_sort;
+ GtkTreePath *source_row;
+
+ selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
+ gtk_tree_selection_get_selected (selection, &model_sort, &iter);
+ source_row_sort = gtk_tree_model_get_path (model_sort, &iter);
+
+ /* Get the unsorted path and model */
+ model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
+ source_row = gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
+ source_row_sort);
+
+ gtk_tree_set_row_drag_data (selection_data,
+ model,
+ source_row);
+
+ gtk_tree_path_free (source_row_sort);
+ gtk_tree_path_free (source_row);
+}
+
+/*
+ * This function receives the data set by the "drag-data-get" signal
+ * handler. This information comes within the #GtkSelectionData. This
+ * function will manage both the drags of folders of the treeview and
+ * drags of headers of the header view widget.
+ */
+static void
+on_drag_data_received (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ GtkSelectionData *selection_data,
+ guint target_type,
+ guint time,
+ gpointer data)
+{
+ GtkWidget *source_widget;
+ GtkTreeModel *model_sort, *dest_model, *source_model;
+ GtkTreePath *source_row, *dest_row, *child_dest_row;
+ GtkTreeViewDropPosition pos;
+ gboolean success = FALSE, delete_source = FALSE;
+
+ /* Do not allow further process */
+ g_signal_stop_emission_by_name (widget, "drag-data-received");
+
+ /* Check if the get_data failed */
+ if (selection_data == NULL || selection_data->length < 0)
+ return;
+
+ /* Get the action */
+ if (context->action == GDK_ACTION_MOVE)
+ delete_source = TRUE;
+
+ /* Get the models */
+ source_widget = gtk_drag_get_source_widget (context);
+ model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
+
+ gtk_tree_get_row_drag_data (selection_data,
+ &source_model,
+ &source_row);
+
+ /* Select the destination model */
+ if (source_widget == widget)
+ dest_model = source_model;
+ else
+ dest_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
+
+
+ /* Get the path to the destination row. Can not call
+ gtk_tree_view_get_drag_dest_row() because the source row
+ it's not selected anymore */
+ gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
+ x, y,
+ &dest_row,
+ &pos);
+
+ /* Only allow drops IN other rows */
+ if (!dest_row ||
+ pos == GTK_TREE_VIEW_DROP_BEFORE ||
+ pos == GTK_TREE_VIEW_DROP_AFTER)
+ goto finish;
+
+ /* Get the destination row in the usorted model */
+ child_dest_row =
+ gtk_tree_model_sort_convert_path_to_child_path (GTK_TREE_MODEL_SORT (model_sort),
+ dest_row);
+ gtk_tree_path_free (dest_row);
+
+ /* Drags from the header view */
+ if ((target_type == TARGET_TREE_ROW) && (source_widget != widget)) {
+
+ /* TODO: do the mail operation */
+
+ success = TRUE;
+ } else {
+ GtkTreeRowReference *source_row_reference;
+ GtkTreeIter parent_iter, iter;
+ TnyFolder *folder;
+ TnyFolderStore *parent_folder;
+ gboolean done;
+
+ /* Check if the drag is possible */
+ if (!gtk_tree_path_compare (source_row, child_dest_row))
+ goto out;
+
+ if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (dest_model),
+ child_dest_row,
+ selection_data))
+ goto out;
+
+ /* Do the mail operation */
+ gtk_tree_model_get_iter (source_model, &parent_iter, child_dest_row);
+ gtk_tree_model_get_iter (source_model, &iter, source_row);
+ gtk_tree_model_get (source_model, &parent_iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+ &parent_folder, -1);
+ gtk_tree_model_get (source_model, &iter,
+ TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN,
+ &folder, -1);
+
+ g_signal_emit (G_OBJECT (widget), signals[FOLDER_MOVED_SIGNAL], 0,
+ folder, parent_folder, &done);
+ if (!done)
+ goto out;
+
+ /* Get a row reference to the source path because the path
+ could change after the insertion. The gtk_drag_finish() is
+ not able to delete the source because that, so we have to
+ do it manually */
+ source_row_reference = gtk_tree_row_reference_new (source_model, source_row);
+ gtk_tree_path_free (source_row);
+
+ /* Insert the dragged row as a child of the dest row */
+ gtk_tree_path_down (child_dest_row);
+ if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (dest_model),
+ child_dest_row,
+ selection_data)) {
+
+ source_row = gtk_tree_row_reference_get_path (source_row_reference);
+
+ success = TRUE;
+ }
+ gtk_tree_row_reference_free (source_row_reference);
+
+ if (folder)
+ g_object_unref (G_OBJECT(folder));
+ if (parent_folder)
+ g_object_unref (G_OBJECT(parent_folder));
+
+ }
+ out:
+ gtk_tree_path_free (child_dest_row);
+
+ /* Save the new path, will be used by the
+ drag-data-delete handler */
+ if (success)
+ g_object_set_data (G_OBJECT (source_widget),
+ ROW_REF_DATA_NAME,
+ gtk_tree_path_copy (source_row));
+
+ finish:
+ gtk_drag_finish (context, success, (success && delete_source), time);
+}
+
+/*
+ * We define a "drag-drop" signal handler because we do not want to
+ * use the default one, because the default one always calls
+ * gtk_drag_finish and we prefer to do it in the "drag-data-received"
+ * signal handler, because there we have all the information available
+ * to know if the dnd was a success or not.
+ */
+static gboolean
+drag_drop_cb (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ guint time,
+ gpointer user_data)
+{
+ if (!context->targets)
+ return FALSE;
+
+ /* Request the data from the source. */
+ gtk_drag_get_data(widget,
+ context,
+ GDK_POINTER_TO_ATOM (context->targets->data),
+ time);
+
+ return TRUE;
+}
+
+/*
+ * This function deletes the data that has been dragged from its
+ * source widget. Since is a function received by the source of the
+ * drag, this function only deletes rows of the folder view
+ * widget. The header view widget will need to define its own one.
+ */
+static void
+drag_data_delete_cb (GtkWidget *widget,
+ GdkDragContext *context,
+ gpointer user_data)
+{
+ GtkTreePath *source_row;
+ GtkTreeModel *model_sort, *model;
+
+ /* Clean dest row */
+ gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget),
+ NULL,
+ GTK_TREE_VIEW_DROP_BEFORE);
+
+ model_sort = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
+ model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model_sort));
+ source_row = g_object_steal_data (G_OBJECT (widget), ROW_REF_DATA_NAME);
+
+ /* Delete the source row */
+ gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
+ source_row);
+
+ gtk_tree_path_free (source_row);
+}
+
+/*
+ * This function expands a node of a tree view if it's not expanded
+ * yet. Not sure why it needs the threads stuff, but gtk+`example code
+ * does that, so that's why they're here.
+ */
+static gint
+expand_row_timeout (gpointer data)
+{
+ GtkTreeView *tree_view = data;
+ GtkTreePath *dest_path = NULL;
+ GtkTreeViewDropPosition pos;
+ gboolean result = FALSE;
+
+ GDK_THREADS_ENTER ();
+
+ gtk_tree_view_get_drag_dest_row (tree_view,
+ &dest_path,
+ &pos);
+
+ if (dest_path &&
+ (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER ||
+ pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)) {
+ gtk_tree_view_expand_row (tree_view, dest_path, FALSE);
+ gtk_tree_path_free (dest_path);
+ }
+ else {
+ if (dest_path)
+ gtk_tree_path_free (dest_path);
+
+ result = TRUE;
+ }
+
+ GDK_THREADS_LEAVE ();
+
+ return result;
+}
+
+/*
+ * This function is called whenever the pointer is moved over a widget
+ * while dragging some data. It installs a timeout that will expand a
+ * node of the treeview if not expanded yet. This function also calls
+ * gdk_drag_status in order to set the suggested action that will be
+ * used by the "drag-data-received" signal handler to know if we
+ * should do a move or just a copy of the data.
+ */
+static gboolean
+on_drag_motion (GtkWidget *widget,
+ GdkDragContext *context,
+ gint x,
+ gint y,
+ guint time,
+ gpointer user_data)
+{
+ GtkTreeViewDropPosition pos;
+ GtkTreePath *dest_row;
+ ModestFolderViewPrivate *priv;
+ GdkDragAction suggested_action;
+
+ priv = MODEST_FOLDER_VIEW_GET_PRIVATE (widget);
+
+ if (priv->timer_expander != 0) {
+ g_source_remove (priv->timer_expander);
+ priv->timer_expander = 0;
+ }
+
+ gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
+ x, y,
+ &dest_row,
+ &pos);
+
+ if (!dest_row)
+ return FALSE;
+
+ /* Expand the selected row after 1/2 second */
+ if (!gtk_tree_view_row_expanded (GTK_TREE_VIEW (widget), dest_row)) {
+ gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), dest_row, pos);
+ priv->timer_expander = g_timeout_add (500, expand_row_timeout, widget);
+ }
+ gtk_tree_path_free (dest_row);
+
+ /* Select the desired action. By default we pick MOVE */
+ suggested_action = GDK_ACTION_MOVE;
+
+ if (context->actions == GDK_ACTION_COPY)
+ gdk_drag_status(context, GDK_ACTION_COPY, time);
+ else if (context->actions == GDK_ACTION_MOVE)
+ gdk_drag_status(context, GDK_ACTION_MOVE, time);
+ else if (context->actions & suggested_action)
+ gdk_drag_status(context, suggested_action, time);
+ else
+ gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
+
+ return TRUE;
+}
+
+/*
+ * This function sets the treeview as a source and a target for dnd
+ * events. It also connects all the requirede signals.
+ */
+static void
+setup_drag_and_drop (GtkTreeView *self)
+{
+ /* Set up the folder view as a dnd destination. Set only the
+ highlight flag, otherwise gtk will have a different
+ behaviour */
+ gtk_drag_dest_set (GTK_WIDGET (self),
+ GTK_DEST_DEFAULT_HIGHLIGHT,
+ drag_types,
+ G_N_ELEMENTS (drag_types),
+ GDK_ACTION_MOVE | GDK_ACTION_COPY);
+
+ gtk_signal_connect(GTK_OBJECT (self),
+ "drag_data_received",
+ GTK_SIGNAL_FUNC(on_drag_data_received),
+ NULL);
+
+
+ /* Set up the treeview as a dnd source */
+ gtk_drag_source_set (GTK_WIDGET (self),
+ GDK_BUTTON1_MASK,
+ drag_types,
+ G_N_ELEMENTS (drag_types),
+ GDK_ACTION_MOVE | GDK_ACTION_COPY);
+
+ gtk_signal_connect(GTK_OBJECT (self),
+ "drag_data_delete",
+ GTK_SIGNAL_FUNC(drag_data_delete_cb),
+ NULL);
+
+ gtk_signal_connect(GTK_OBJECT (self),
+ "drag_motion",
+ GTK_SIGNAL_FUNC(on_drag_motion),
+ NULL);
+
+
+ gtk_signal_connect(GTK_OBJECT (self),
+ "drag_data_get",
+ GTK_SIGNAL_FUNC(on_drag_data_get),
+ NULL);
+
+ gtk_signal_connect(GTK_OBJECT (self),
+ "drag_drop",
+ GTK_SIGNAL_FUNC(drag_drop_cb),
+ NULL);
+}