summaryrefslogtreecommitdiff
path: root/src/nautilus-list-view-dnd.c
blob: d682b3a02f0357b11edd7d8f053b745c10819ab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* nautilus-list-view-dnd.c
 *
 * Copyright (C) 2015 Carlos Soriano <csoriano@gnome.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "nautilus-list-view-dnd.h"
#include "nautilus-list-view-private.h"

#include "nautilus-dnd.h"

static GtkTargetList *          source_target_list = NULL;

static void
drag_data_get_callback (GtkWidget        *widget,
                        GdkDragContext   *context,
                        GtkSelectionData *selection_data,
                        guint             info,
                        guint             time)
{
  GtkTreeView *tree_view;
  GtkTreeModel *model;
  GList *selection_cache;

  tree_view = GTK_TREE_VIEW (widget);

  model = gtk_tree_view_get_model (tree_view);

  if (model == NULL)
    return;

  selection_cache = g_object_get_data (G_OBJECT (context), "drag-info");
  if (selection_cache == NULL)
    return;

  nautilus_drag_drag_data_get_from_cache (selection_cache, context, selection_data, info, time);
}

static cairo_surface_t *
get_drag_surface (NautilusListView *view)
{
  GtkTreeModel *model;
  GtkTreePath *path;
  GtkTreeIter iter;
  cairo_surface_t *ret;
  GdkRectangle cell_area;

  ret = NULL;

  if (gtk_tree_view_get_path_at_pos (view->details->tree_view,
      view->details->drag_x,
      view->details->drag_y,
      &path, NULL, NULL, NULL))
    {
      model = gtk_tree_view_get_model (view->details->tree_view);
      gtk_tree_model_get_iter (model, &iter, path);
      gtk_tree_model_get (model, &iter,
                          nautilus_list_model_get_column_id_from_zoom_level (view->details->zoom_level),
                          &ret,
                          -1);
    }

  gtk_tree_view_get_cell_area (view->details->tree_view,
                               path,
                               view->details->file_name_column,
                               &cell_area);

  gtk_tree_path_free (path);

  return ret;
}

/* iteration glue struct */
typedef struct {
  NautilusListView *view;
  NautilusDragEachSelectedItemDataGet iteratee;
  gpointer iteratee_data;
} ListGetDataBinderContext;

static void
item_get_data_binder (GtkTreeModel *model,
                      GtkTreePath  *path,
                      GtkTreeIter  *iter,
                      gpointer      data)
{
  ListGetDataBinderContext *context = data;
  NautilusFile *file;
  GtkTreeView *treeview;
  GtkTreeViewColumn *column;
  GdkRectangle cell_area;
  int drag_begin_y  = 0;
  char *uri;

  treeview = nautilus_list_model_get_drag_view (context->view->details->model,
                                                NULL,
                                                &drag_begin_y);
  column = gtk_tree_view_get_column (treeview, 0);

  file = nautilus_list_model_file_for_path (NAUTILUS_LIST_MODEL (model), path);
  if (file == NULL)
    return;

  gtk_tree_view_get_cell_area (treeview,
                               path,
                               column,
                               &cell_area);

  if (nautilus_file_is_nautilus_link (file))
      uri = nautilus_file_get_uri (file);
  else
      uri = nautilus_file_get_activation_uri (file);

  nautilus_file_unref (file);

  /* pass the uri, mouse-relative x/y and icon width/height */
  context->iteratee (uri,
                     0,
                     cell_area.y - drag_begin_y,
                     cell_area.width,
                     cell_area.height,
                     context->iteratee_data);

  g_free (uri);
}

static void
each_item_get_data_binder (NautilusDragEachSelectedItemDataGet iteratee,
                           gpointer                            iterator_context,
                           gpointer                            data)
{
  NautilusListView *view = NAUTILUS_LIST_VIEW (iterator_context);
  ListGetDataBinderContext context;
  GtkTreeSelection *selection;

  context.view = view;
  context.iteratee = iteratee;
  context.iteratee_data = data;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view->details->tree_view));
  gtk_tree_selection_selected_foreach (selection, item_get_data_binder, &context);
}

static void
drag_begin_callback (GtkWidget        *widget,
                     GdkDragContext   *context,
                     NautilusListView *view)
{
  GList *selection_cache;
  cairo_surface_t *surface;

  surface = get_drag_surface (view);
  if (surface)
    {
      gtk_drag_set_icon_surface (context, surface);
      cairo_surface_destroy (surface);
    }
  else
    {
      gtk_drag_set_icon_default (context);
    }

  view->details->drag_button = 0;
  view->details->drag_started = TRUE;

  selection_cache = nautilus_drag_create_selection_cache (view,
                                                          each_item_get_data_binder);

  g_object_set_data_full (G_OBJECT (context),
                          "drag-info",
                          selection_cache,
                          (GDestroyNotify)nautilus_drag_destroy_selection_list);
}

void
nautilus_list_view_dnd_init (NautilusListView *list_view)
{
  g_signal_connect_object (list_view->details->tree_view, "drag-begin",
                           G_CALLBACK (drag_begin_callback), list_view, 0);
  g_signal_connect_object (list_view->details->tree_view, "drag-data-get",
                           G_CALLBACK (drag_data_get_callback), list_view, 0);
}

gboolean
nautilus_list_view_dnd_drag_begin (NautilusListView *list_view,
                                   GdkEventMotion   *event)
{
  if (list_view->details->drag_button != 0)
    {
      if (!source_target_list)
        source_target_list = nautilus_list_model_get_drag_target_list ();

      if (gtk_drag_check_threshold (GTK_WIDGET (list_view->details->tree_view),
                                    list_view->details->drag_x,
                                    list_view->details->drag_y,
                                    event->x,
                                    event->y))
        {
          gtk_drag_begin_with_coordinates (GTK_WIDGET (list_view->details->tree_view),
                                           source_target_list,
                                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_LINK | GDK_ACTION_ASK,
                                           list_view->details->drag_button,
                                           (GdkEvent*)event,
                                           -1,
                                           -1);
        }
      return TRUE;
    }

  return FALSE;
}