gimpcontainertreeview-dnd.c 16.1 KB
Newer Older
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
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpcontainertreeview-dnd.c
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "widgets-types.h"

#include "core/gimpcontainer.h"
#include "core/gimpviewable.h"

#include "gimpcontainertreeview.h"
#include "gimpcontainertreeview-dnd.h"
33
#include "gimpcontainerview.h"
34 35
#include "gimpdnd.h"
#include "gimppreviewrenderer.h"
36
#include "gimpselectiondata.h"
37 38 39 40 41 42 43 44 45


static gboolean
gimp_container_tree_view_drop_status (GimpContainerTreeView    *tree_view,
                                      GdkDragContext           *context,
                                      gint                      x,
                                      gint                      y,
                                      guint                     time,
                                      GtkTreePath             **return_path,
46 47
                                      GdkAtom                  *return_atom,
                                      GimpDndType              *return_src_type,
48 49 50 51
                                      GimpViewable            **return_src,
                                      GimpViewable            **return_dest,
                                      GtkTreeViewDropPosition  *return_pos)
{
52 53 54 55 56
  GimpViewable  *src_viewable = NULL;
  GtkTreePath   *path;
  GtkTargetList *target_list;
  GdkAtom        target_atom;
  GimpDndType    src_type;
57

58
  if (! gimp_container_view_get_reorderable (GIMP_CONTAINER_VIEW (tree_view)))
59 60
    return FALSE;

61 62 63 64
  target_list = gtk_drag_dest_get_target_list (GTK_WIDGET (tree_view->view));
  target_atom = gtk_drag_dest_find_target (GTK_WIDGET (tree_view->view),
                                           context, target_list);
  if (! gtk_target_list_find (target_list, target_atom, &src_type))
65 66
    return FALSE;

67 68 69 70 71 72 73 74 75 76 77 78 79
  switch (src_type)
    {
    case GIMP_DND_TYPE_URI_LIST:
    case GIMP_DND_TYPE_TEXT_PLAIN:
    case GIMP_DND_TYPE_NETSCAPE_URL:
    case GIMP_DND_TYPE_COLOR:
    case GIMP_DND_TYPE_SVG:
    case GIMP_DND_TYPE_SVG_XML:
      break;

    default:
      {
        GtkWidget *src_widget;
80

81 82 83 84 85 86 87 88 89 90 91 92
        src_widget = gtk_drag_get_source_widget (context);

        if (! src_widget)
          return FALSE;

        src_viewable = gimp_dnd_get_drag_data (src_widget);

        if (! GIMP_IS_VIEWABLE (src_viewable))
          return FALSE;
      }
      break;
    }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  if (gtk_tree_view_get_path_at_pos (tree_view->view, x, y,
                                     &path, NULL, NULL, NULL))
    {
      GimpPreviewRenderer     *renderer;
      GimpViewable            *dest_viewable;
      GtkTreeIter              iter;
      GdkRectangle             cell_area;
      GtkTreeViewDropPosition  drop_pos;
      GdkDragAction            drag_action;

      gtk_tree_model_get_iter (tree_view->model, &iter, path);

      gtk_tree_model_get (tree_view->model, &iter,
                          tree_view->model_column_renderer, &renderer,
                          -1);

      dest_viewable = renderer->viewable;

112 113
      g_object_unref (renderer);

114 115 116
      gtk_tree_view_get_cell_area (tree_view->view, path, NULL, &cell_area);

      if (y >= (cell_area.y + cell_area.height / 2))
117 118 119
        {
          drop_pos = GTK_TREE_VIEW_DROP_AFTER;
        }
120
      else
121 122 123
        {
          drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
        }
124 125

      if (GIMP_CONTAINER_TREE_VIEW_GET_CLASS (tree_view)->drop_possible (tree_view,
126
                                                                         src_type,
127 128 129
                                                                         src_viewable,
                                                                         dest_viewable,
                                                                         drop_pos,
130
                                                                         &drop_pos,
131 132 133 134 135 136 137 138 139
                                                                         &drag_action))
        {
          gdk_drag_status (context, drag_action, time);

          if (return_path)
            *return_path = path;
          else
            gtk_tree_path_free (path);

140 141 142
          if (return_atom)
            *return_atom = target_atom;

143 144 145 146 147 148 149 150 151 152 153 154
          if (return_src)
            *return_src = src_viewable;

          if (return_dest)
            *return_dest = dest_viewable;

          if (return_pos)
            *return_pos = drop_pos;

          return TRUE;
        }

155
      gdk_drag_status (context, 0, time);
156 157 158 159 160 161 162

      gtk_tree_path_free (path);
    }

  return FALSE;
}

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
#define SCROLL_DISTANCE 30
#define SCROLL_STEP     10
#define SCROLL_INTERVAL  5
/* #define SCROLL_DEBUG 1 */

static gboolean
gimp_container_tree_view_scroll_timeout (gpointer data)
{
  GimpContainerTreeView *tree_view = GIMP_CONTAINER_TREE_VIEW (data);
  GtkAdjustment         *adj;
  gdouble                new_value;

  adj = gtk_tree_view_get_vadjustment (GTK_TREE_VIEW (tree_view->view));

#ifdef SCROLL_DEBUG
  g_print ("scroll_timeout: scrolling by %d\n", SCROLL_STEP);
#endif

  if (tree_view->scroll_dir == GDK_SCROLL_UP)
    new_value = adj->value - SCROLL_STEP;
  else
    new_value = adj->value + SCROLL_STEP;

  new_value = CLAMP (new_value, adj->lower, adj->upper - adj->page_size);

  gtk_adjustment_set_value (adj, new_value);

  if (tree_view->scroll_timeout_id)
    {
      g_source_remove (tree_view->scroll_timeout_id);

      tree_view->scroll_timeout_id =
        g_timeout_add (tree_view->scroll_timeout_interval,
                       gimp_container_tree_view_scroll_timeout,
                       tree_view);
    }

  return FALSE;
}

203 204 205 206 207 208
void
gimp_container_tree_view_drag_leave (GtkWidget             *widget,
                                     GdkDragContext        *context,
                                     guint                  time,
                                     GimpContainerTreeView *tree_view)
{
209 210 211 212 213 214
  if (tree_view->scroll_timeout_id)
    {
      g_source_remove (tree_view->scroll_timeout_id);
      tree_view->scroll_timeout_id = 0;
    }

215 216 217 218 219 220 221 222 223 224 225 226 227 228
  gtk_tree_view_unset_rows_drag_dest (tree_view->view);
}

gboolean
gimp_container_tree_view_drag_motion (GtkWidget             *widget,
                                      GdkDragContext        *context,
                                      gint                   x,
                                      gint                   y,
                                      guint                  time,
                                      GimpContainerTreeView *tree_view)
{
  GtkTreePath             *path;
  GtkTreeViewDropPosition  drop_pos;

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  if (y < SCROLL_DISTANCE || y > (widget->allocation.height - SCROLL_DISTANCE))
    {
      gint distance;

      if (y < SCROLL_DISTANCE)
        {
          tree_view->scroll_dir = GDK_SCROLL_UP;
          distance = MIN (-y, -1);
        }
      else
        {
          tree_view->scroll_dir = GDK_SCROLL_DOWN;
          distance = MAX (widget->allocation.height - y, 1);
        }

      tree_view->scroll_timeout_interval = SCROLL_INTERVAL * ABS (distance);

#ifdef SCROLL_DEBUG
      g_print ("drag_motion: scroll_distance = %d  scroll_interval = %d\n",
               distance, tree_view->scroll_timeout_interval);
#endif

      if (! tree_view->scroll_timeout_id)
        tree_view->scroll_timeout_id =
          g_timeout_add (tree_view->scroll_timeout_interval,
                         gimp_container_tree_view_scroll_timeout,
                         tree_view);
    }
  else if (tree_view->scroll_timeout_id)
    {
      g_source_remove (tree_view->scroll_timeout_id);
      tree_view->scroll_timeout_id = 0;
    }

263 264
  if (gimp_container_tree_view_drop_status (tree_view,
                                            context, x, y, time,
265 266
                                            &path, NULL, NULL, NULL, NULL,
                                            &drop_pos))
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    {
      gtk_tree_view_set_drag_dest_row (tree_view->view, path, drop_pos);
      gtk_tree_path_free (path);

      return TRUE;
    }

  gtk_tree_view_unset_rows_drag_dest (tree_view->view);

  return FALSE;
}

gboolean
gimp_container_tree_view_drag_drop (GtkWidget             *widget,
                                    GdkDragContext        *context,
                                    gint                   x,
                                    gint                   y,
                                    guint                  time,
                                    GimpContainerTreeView *tree_view)
{
287
  GimpDndType              src_type;
288 289
  GimpViewable            *src_viewable;
  GimpViewable            *dest_viewable;
290
  GdkAtom                  target;
291 292
  GtkTreeViewDropPosition  drop_pos;

293 294 295 296 297 298
  if (tree_view->scroll_timeout_id)
    {
      g_source_remove (tree_view->scroll_timeout_id);
      tree_view->scroll_timeout_id = 0;
    }

299 300
  if (gimp_container_tree_view_drop_status (tree_view,
                                            context, x, y, time,
301 302 303
                                            NULL, &target, &src_type,
                                            &src_viewable,
                                            &dest_viewable, &drop_pos))
304
    {
305 306 307 308 309
      GimpContainerTreeViewClass *tree_view_class;

      tree_view_class = GIMP_CONTAINER_TREE_VIEW_GET_CLASS (tree_view);

      if (src_viewable)
310 311
        tree_view_class->drop_viewable (tree_view, src_viewable,
                                        dest_viewable, drop_pos);
312 313
      else
        gtk_drag_get_data (widget, context, target, time);
314 315 316 317

      return TRUE;
    }

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  return FALSE;
}

void
gimp_container_tree_view_drag_data_received (GtkWidget             *widget,
                                             GdkDragContext        *context,
                                             gint                   x,
                                             gint                   y,
                                             GtkSelectionData      *selection_data,
                                             guint                  info,
                                             guint                  time,
                                             GimpContainerTreeView *tree_view)
{
  GimpViewable            *dest_viewable;
  GtkTreeViewDropPosition  drop_pos;
  gboolean                 success = FALSE;

  if (gimp_container_tree_view_drop_status (tree_view,
                                            context, x, y, time,
                                            NULL, NULL, NULL, NULL,
                                            &dest_viewable, &drop_pos))
    {
      GimpContainerTreeViewClass *tree_view_class;

      tree_view_class = GIMP_CONTAINER_TREE_VIEW_GET_CLASS (tree_view);

      switch (info)
        {
        case GIMP_DND_TYPE_URI_LIST:
        case GIMP_DND_TYPE_TEXT_PLAIN:
        case GIMP_DND_TYPE_NETSCAPE_URL:
349
          if (tree_view_class->drop_uri_list)
350 351 352
            {
              GList *uri_list;

353
              uri_list = gimp_selection_data_get_uri_list (selection_data);
354 355 356

              if (uri_list)
                {
357 358
                  tree_view_class->drop_uri_list (tree_view, uri_list,
                                                  dest_viewable, drop_pos);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

                  g_list_foreach (uri_list, (GFunc) g_free, NULL);
                  g_list_free (uri_list);

                  success = TRUE;
                }
            }
          break;

        case GIMP_DND_TYPE_COLOR:
          if (tree_view_class->drop_color)
            {
              GimpRGB color;

              if (gimp_selection_data_get_color (selection_data, &color))
                {
                  tree_view_class->drop_color (tree_view, &color,
                                               dest_viewable, drop_pos);

                  success = TRUE;
                }
            }
          break;

        case GIMP_DND_TYPE_SVG:
        case GIMP_DND_TYPE_SVG_XML:
          if (tree_view_class->drop_svg)
            {
387 388
              const guchar *stream;
              gsize         stream_length;
389

390 391
              stream = gimp_selection_data_get_stream (selection_data,
                                                       &stream_length);
392

393
              if (stream)
394 395
                {
                  tree_view_class->drop_svg (tree_view,
396 397
                                             (const gchar *) stream,
                                             stream_length,
398 399 400 401 402 403 404 405 406 407 408 409 410
                                             dest_viewable, drop_pos);

                  success = TRUE;
                }
            }
          break;

        default:
          break;
        }
    }

  gtk_drag_finish (context, success, FALSE, time);
411 412 413 414
}

gboolean
gimp_container_tree_view_real_drop_possible (GimpContainerTreeView   *tree_view,
415
                                             GimpDndType              src_type,
416 417 418
                                             GimpViewable            *src_viewable,
                                             GimpViewable            *dest_viewable,
                                             GtkTreeViewDropPosition  drop_pos,
419 420
                                             GtkTreeViewDropPosition *return_drop_pos,
                                             GdkDragAction           *return_drag_action)
421
{
422 423 424 425
  GimpContainerView *view       = GIMP_CONTAINER_VIEW (tree_view);
  GimpContainer     *container  = gimp_container_view_get_container (view);
  gint               src_index  = -1;
  gint               dest_index = -1;
426

427 428 429 430 431
  if (src_viewable && g_type_is_a (G_TYPE_FROM_INSTANCE (src_viewable),
                                   container->children_type))
    {
      if (src_viewable == dest_viewable)
        return FALSE;
432

433 434 435 436
      src_index  = gimp_container_get_child_index (container,
                                                   GIMP_OBJECT (src_viewable));
      dest_index = gimp_container_get_child_index (container,
                                                   GIMP_OBJECT (dest_viewable));
437

438 439 440
      if (src_index == -1 || dest_index == -1)
        return FALSE;
    }
441 442 443 444 445 446 447 448 449 450 451 452

  if (drop_pos == GTK_TREE_VIEW_DROP_BEFORE)
    {
      if (dest_index == (src_index + 1))
        return FALSE;
    }
  else
    {
      if (dest_index == (src_index - 1))
        return FALSE;
    }

453 454 455 456 457 458 459 460 461 462 463
  if (return_drop_pos)
    *return_drop_pos = drop_pos;

  if (return_drag_action)
    {
      if (src_viewable && g_type_is_a (G_TYPE_FROM_INSTANCE (src_viewable),
                                       container->children_type))
        *return_drag_action = GDK_ACTION_MOVE;
      else
        *return_drag_action = GDK_ACTION_COPY;
    }
464 465 466 467 468

  return TRUE;
}

void
469 470 471 472
gimp_container_tree_view_real_drop_viewable (GimpContainerTreeView   *tree_view,
                                             GimpViewable            *src_viewable,
                                             GimpViewable            *dest_viewable,
                                             GtkTreeViewDropPosition  drop_pos)
473
{
474 475
  GimpContainerView *view      = GIMP_CONTAINER_VIEW (tree_view);
  GimpContainer     *container = gimp_container_view_get_container (view);
476 477 478
  gint               src_index;
  gint               dest_index;

479 480 481 482
  src_index  = gimp_container_get_child_index (container,
                                               GIMP_OBJECT (src_viewable));
  dest_index = gimp_container_get_child_index (container,
                                               GIMP_OBJECT (dest_viewable));
483 484 485 486 487 488 489 490 491 492

  if (drop_pos == GTK_TREE_VIEW_DROP_AFTER && src_index > dest_index)
    {
      dest_index++;
    }
  else if (drop_pos == GTK_TREE_VIEW_DROP_BEFORE && src_index < dest_index)
    {
      dest_index--;
    }

493
  gimp_container_reorder (container, GIMP_OBJECT (src_viewable), dest_index);
494
}