gtkdnd.c 119 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2
 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 4
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18 19
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "config.h"
28 29 30 31

#include <stdlib.h>
#include <string.h>

32 33
#include "gdkconfig.h"

34
#include "gdk/gdkkeysyms.h"
35

36 37 38 39 40 41
#ifdef GDK_WINDOWING_X11
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include "gdk/x11/gdkx.h"
#endif

42
#include "gtkdnd.h"
43 44
#include "gtkiconfactory.h"
#include "gtkicontheme.h"
45
#include "gtkimage.h"
46 47
#include "gtkinvisible.h"
#include "gtkmain.h"
48
#include "gtkplug.h"
49
#include "gtkstock.h"
Owen Taylor's avatar
Owen Taylor committed
50
#include "gtkwindow.h"
Matthias Clasen's avatar
Matthias Clasen committed
51
#include "gtkintl.h"
52
#include "gtkdndcursors.h"
53
#include "gtkalias.h"
54

55
static GSList *source_widgets = NULL;
56 57 58 59 60 61 62 63 64

typedef struct _GtkDragSourceSite GtkDragSourceSite;
typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
typedef struct _GtkDragDestSite GtkDragDestSite;
typedef struct _GtkDragDestInfo GtkDragDestInfo;
typedef struct _GtkDragAnim GtkDragAnim;
typedef struct _GtkDragFindData GtkDragFindData;


Owen Taylor's avatar
Owen Taylor committed
65 66
typedef enum 
{
67 68 69 70 71
  GTK_DRAG_STATUS_DRAG,
  GTK_DRAG_STATUS_WAIT,
  GTK_DRAG_STATUS_DROP
} GtkDragStatus;

Owen Taylor's avatar
Owen Taylor committed
72 73
struct _GtkDragSourceSite 
{
74 75 76
  GdkModifierType    start_button_mask;
  GtkTargetList     *target_list;        /* Targets for drag data */
  GdkDragAction      actions;            /* Possible actions */
77 78 79 80 81 82 83 84

  /* Drag icon */
  GtkImageType icon_type;
  union
  {
    GtkImagePixmapData pixmap;
    GtkImagePixbufData pixbuf;
    GtkImageStockData stock;
85
    GtkImageIconNameData name;
86
  } icon_data;
Havoc Pennington's avatar
Havoc Pennington committed
87
  GdkBitmap *icon_mask;
88

89
  GdkColormap       *colormap;	         /* Colormap for drag icon */
Owen Taylor's avatar
Owen Taylor committed
90

91 92 93 94
  /* Stored button press information to detect drag beginning */
  gint               state;
  gint               x, y;
};
Owen Taylor's avatar
Owen Taylor committed
95
  
Owen Taylor's avatar
Owen Taylor committed
96 97
struct _GtkDragSourceInfo 
{
98 99
  GtkWidget         *widget;
  GtkTargetList     *target_list; /* Targets for drag data */
100
  GdkDragAction      possible_actions; /* Actions allowed by source */
101 102
  GdkDragContext    *context;	  /* drag context */
  GtkWidget         *icon_window; /* Window for drag */
103
  GtkWidget         *fallback_icon; /* Window for drag used on other screens */
104 105 106 107
  GtkWidget         *ipc_widget;  /* GtkInvisible for grab, message passing */
  GdkCursor         *cursor;	  /* Cursor for drag */
  gint hot_x, hot_y;		  /* Hot spot for drag */
  gint button;			  /* mouse button starting drag */
Owen Taylor's avatar
Owen Taylor committed
108

109
  GtkDragStatus      status;	  /* drag status */
110
  GdkEvent          *last_event;  /* pending event */
Owen Taylor's avatar
Owen Taylor committed
111

112 113
  gint               start_x, start_y; /* Initial position */
  gint               cur_x, cur_y;     /* Current Position */
114
  GdkScreen         *cur_screen;       /* Current screen for pointer */
Owen Taylor's avatar
Owen Taylor committed
115

116
  guint32            grab_time;   /* timestamp for initial grab */
117 118 119
  GList             *selections;  /* selections we've claimed */
  
  GtkDragDestInfo   *proxy_dest;  /* Set if this is a proxy drag */
Owen Taylor's avatar
Owen Taylor committed
120

121
  guint              update_idle;      /* Idle function to update the drag */
Owen Taylor's avatar
Owen Taylor committed
122 123
  guint              drop_timeout;     /* Timeout for aborting drop */
  guint              destroy_icon : 1; /* If true, destroy icon_window
124
      				        */
125
  guint              have_grab : 1;    /* Do we still have the pointer grab
126
				        */
127 128
  GdkPixbuf         *icon_pixbuf;
  GdkCursor         *drag_cursors[6];
129 130
};

Owen Taylor's avatar
Owen Taylor committed
131 132
struct _GtkDragDestSite 
{
133 134 135 136 137
  GtkDestDefaults    flags;
  GtkTargetList     *target_list;
  GdkDragAction      actions;
  GdkWindow         *proxy_window;
  GdkDragProtocol    proxy_protocol;
138 139 140
  guint              do_proxy : 1;
  guint              proxy_coords : 1;
  guint              have_drag : 1;
141
  guint              track_motion : 1;
142
};
Owen Taylor's avatar
Owen Taylor committed
143
  
Owen Taylor's avatar
Owen Taylor committed
144 145
struct _GtkDragDestInfo 
{
146 147 148 149
  GtkWidget         *widget;	   /* Widget in which drag is in */
  GdkDragContext    *context;	   /* Drag context */
  GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
  GtkSelectionData  *proxy_data;   /* Set while retrieving proxied data */
150
  guint              dropped : 1;     /* Set after we receive a drop */
151
  guint32            proxy_drop_time; /* Timestamp for proxied drop */
152
  guint              proxy_drop_wait : 1; /* Set if we are waiting for a
153 154 155 156 157 158
					   * status reply before sending
					   * a proxied drop on.
					   */
  gint               drop_x, drop_y; /* Position of drop */
};

159
#define DROP_ABORT_TIME 300000
160 161 162 163 164 165

#define ANIM_STEP_TIME 50
#define ANIM_STEP_LENGTH 50
#define ANIM_MIN_STEPS 5
#define ANIM_MAX_STEPS 10

Owen Taylor's avatar
Owen Taylor committed
166 167
struct _GtkDragAnim 
{
168 169 170 171 172
  GtkDragSourceInfo *info;
  gint step;
  gint n_steps;
};

Owen Taylor's avatar
Owen Taylor committed
173 174
struct _GtkDragFindData 
{
175 176 177 178 179
  gint x;
  gint y;
  GdkDragContext *context;
  GtkDragDestInfo *info;
  gboolean found;
180
  gboolean toplevel;
181 182 183 184 185 186 187
  gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
			gint x, gint y, guint32 time);
  guint32 time;
};

/* Enumeration for some targets we handle internally */

Owen Taylor's avatar
Owen Taylor committed
188
enum {
189
  TARGET_MOTIF_SUCCESS = 0x40000000,
190 191 192 193 194 195 196 197 198 199 200 201 202
  TARGET_MOTIF_FAILURE,
  TARGET_DELETE
};

/* Drag icons */

static GdkPixmap   *default_icon_pixmap = NULL;
static GdkPixmap   *default_icon_mask = NULL;
static GdkColormap *default_icon_colormap = NULL;
static gint         default_icon_hot_x;
static gint         default_icon_hot_y;

/* Forward declarations */
Owen Taylor's avatar
Owen Taylor committed
203 204 205 206 207
static void          gtk_drag_get_event_actions (GdkEvent        *event, 
					         gint             button,
					         GdkDragAction    actions,
					         GdkDragAction   *suggested_action,
					         GdkDragAction   *possible_actions);
208
static GdkCursor *   gtk_drag_get_cursor         (GdkDisplay     *display,
209 210
						  GdkDragAction   action,
						  GtkDragSourceInfo *info);
211
static void          gtk_drag_update_cursor      (GtkDragSourceInfo *info);
Matthias Clasen's avatar
Matthias Clasen committed
212 213
static GtkWidget    *gtk_drag_get_ipc_widget            (GtkWidget *widget);
static GtkWidget    *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
Owen Taylor's avatar
Owen Taylor committed
214
static void          gtk_drag_release_ipc_widget (GtkWidget      *widget);
215

Owen Taylor's avatar
Owen Taylor committed
216 217 218
static gboolean      gtk_drag_highlight_expose   (GtkWidget      *widget,
					  	  GdkEventExpose *event,
						  gpointer        data);
219

220 221
static void     gtk_drag_selection_received     (GtkWidget        *widget,
						 GtkSelectionData *selection_data,
222
						 guint             time,
223 224 225 226 227 228 229 230
						 gpointer          data);
static void     gtk_drag_find_widget            (GtkWidget        *widget,
						 GtkDragFindData  *data);
static void     gtk_drag_proxy_begin            (GtkWidget        *widget,
						 GtkDragDestInfo  *dest_info,
						 guint32           time);
static void     gtk_drag_dest_realized          (GtkWidget        *widget);
static void     gtk_drag_dest_hierarchy_changed (GtkWidget        *widget,
Owen Taylor's avatar
Owen Taylor committed
231
						 GtkWidget        *previous_toplevel);
232 233 234 235 236 237 238 239 240 241 242 243 244 245
static void     gtk_drag_dest_site_destroy      (gpointer          data);
static void     gtk_drag_dest_leave             (GtkWidget        *widget,
						 GdkDragContext   *context,
						 guint             time);
static gboolean gtk_drag_dest_motion            (GtkWidget        *widget,
						 GdkDragContext   *context,
						 gint              x,
						 gint              y,
						 guint             time);
static gboolean gtk_drag_dest_drop              (GtkWidget        *widget,
						 GdkDragContext   *context,
						 gint              x,
						 gint              y,
						 guint             time);
Owen Taylor's avatar
Owen Taylor committed
246 247 248 249 250 251

static GtkDragDestInfo *  gtk_drag_get_dest_info     (GdkDragContext *context,
						      gboolean        create);
static GtkDragSourceInfo *gtk_drag_get_source_info   (GdkDragContext *context,
						      gboolean        create);
static void               gtk_drag_clear_source_info (GdkDragContext *context);
252 253 254 255 256 257 258 259 260

static void gtk_drag_source_check_selection    (GtkDragSourceInfo *info, 
					        GdkAtom            selection,
					        guint32            time);
static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
						guint32            time);
static void gtk_drag_drop                      (GtkDragSourceInfo *info,
						guint32            time);
static void gtk_drag_drop_finished             (GtkDragSourceInfo *info,
261
						GtkDragResult      result,
262
						guint              time);
263
static void gtk_drag_cancel                    (GtkDragSourceInfo *info,
264
						GtkDragResult      result,
265
						guint32            time);
266

267
static gboolean gtk_drag_source_event_cb       (GtkWidget         *widget,
268 269 270 271 272 273 274 275
						GdkEvent          *event,
						gpointer           data);
static void gtk_drag_source_site_destroy       (gpointer           data);
static void gtk_drag_selection_get             (GtkWidget         *widget, 
						GtkSelectionData  *selection_data,
						guint              sel_info,
						guint32            time,
						gpointer           data);
276
static gboolean gtk_drag_anim_timeout          (gpointer           data);
277
static void gtk_drag_remove_icon               (GtkDragSourceInfo *info);
Owen Taylor's avatar
Owen Taylor committed
278
static void gtk_drag_source_info_destroy       (GtkDragSourceInfo *info);
279 280
static void gtk_drag_add_update_idle           (GtkDragSourceInfo *info);

281
static void gtk_drag_update                    (GtkDragSourceInfo *info,
282
						GdkScreen         *screen,
283 284 285
						gint               x_root,
						gint               y_root,
						GdkEvent          *event);
286
static gboolean gtk_drag_motion_cb             (GtkWidget         *widget, 
287 288
					        GdkEventMotion    *event, 
					        gpointer           data);
289
static gboolean gtk_drag_key_cb                (GtkWidget         *widget, 
290 291
					        GdkEventKey       *event, 
					        gpointer           data);
292 293 294
static gboolean gtk_drag_grab_broken_event_cb  (GtkWidget          *widget,
						GdkEventGrabBroken *event,
						gpointer            data);
295 296 297
static void     gtk_drag_grab_notify_cb        (GtkWidget         *widget,
						gboolean           was_grabbed,
						gpointer           data);
298
static gboolean gtk_drag_button_release_cb     (GtkWidget         *widget, 
299 300
					        GdkEventButton    *event, 
					        gpointer           data);
301
static gboolean gtk_drag_abort_timeout         (gpointer           data);
302

303 304 305 306 307 308 309
static void     set_icon_stock_pixbuf          (GdkDragContext    *context,
						const gchar       *stock_id,
						GdkPixbuf         *pixbuf,
						gint               hot_x,
						gint               hot_y,
						gboolean           force_window);

310 311 312 313
/************************
 * Cursor and Icon data *
 ************************/

Owen Taylor's avatar
Owen Taylor committed
314
static struct {
315
  GdkDragAction action;
316 317 318
  const gchar  *name;
  const guint8 *data;
  GdkPixbuf    *pixbuf;
319 320
  GdkCursor    *cursor;
} drag_cursors[] = {
Matthias Clasen's avatar
Matthias Clasen committed
321
  { GDK_ACTION_DEFAULT, NULL },
322 323 324 325 326
  { GDK_ACTION_ASK,   "dnd-ask",  dnd_cursor_ask,  NULL, NULL },
  { GDK_ACTION_COPY,  "dnd-copy", dnd_cursor_copy, NULL, NULL },
  { GDK_ACTION_MOVE,  "dnd-move", dnd_cursor_move, NULL, NULL },
  { GDK_ACTION_LINK,  "dnd-link", dnd_cursor_link, NULL, NULL },
  { 0              ,  "dnd-none", dnd_cursor_none, NULL, NULL },
327 328
};

Owen Taylor's avatar
Owen Taylor committed
329
static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
330 331 332 333 334

/*********************
 * Utility functions *
 *********************/

Manish Singh's avatar
Manish Singh committed
335
static void
336 337 338 339 340
set_can_change_screen (GtkWidget *widget,
		       gboolean   can_change_screen)
{
  can_change_screen = can_change_screen != FALSE;
  
Matthias Clasen's avatar
Matthias Clasen committed
341
  g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
342 343 344 345 346 347 348 349 350 351
		     GUINT_TO_POINTER (can_change_screen));
}

static gboolean
get_can_change_screen (GtkWidget *widget)
{
  return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;

}

Owen Taylor's avatar
Owen Taylor committed
352
static GtkWidget *
Matthias Clasen's avatar
Matthias Clasen committed
353
gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
354 355
{
  GtkWidget *result;
356 357
  GSList *drag_widgets = g_object_get_data (G_OBJECT (screen), 
					    "gtk-dnd-ipc-widgets");
358 359 360 361 362 363
  
  if (drag_widgets)
    {
      GSList *tmp = drag_widgets;
      result = drag_widgets->data;
      drag_widgets = drag_widgets->next;
364
      g_object_set_data (G_OBJECT (screen),
Matthias Clasen's avatar
Matthias Clasen committed
365
			 I_("gtk-dnd-ipc-widgets"),
366
			 drag_widgets);
367 368 369 370
      g_slist_free_1 (tmp);
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
371 372 373 374
      result = gtk_window_new (GTK_WINDOW_POPUP);
      gtk_window_set_screen (GTK_WINDOW (result), screen);
      gtk_window_resize (GTK_WINDOW (result), 1, 1);
      gtk_window_move (GTK_WINDOW (result), -100, -100);
375
      gtk_widget_show (result);
Matthias Clasen's avatar
Matthias Clasen committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    }  

  return result;
}

static GtkWidget *
gtk_drag_get_ipc_widget (GtkWidget *widget)
{
  GtkWidget *result;
  GtkWidget *toplevel;

  result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
  
  toplevel = gtk_widget_get_toplevel (widget);
  
  if (GTK_IS_WINDOW (toplevel))
    {
      if (GTK_WINDOW (toplevel)->group)
	gtk_window_group_add_window (GTK_WINDOW (toplevel)->group, 
                                     GTK_WINDOW (result));
396
    }
Owen Taylor's avatar
Owen Taylor committed
397

398 399 400
  return result;
}

Matthias Clasen's avatar
Matthias Clasen committed
401

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
#ifdef GDK_WINDOWING_X11

/*
 * We want to handle a handful of keys during DND, e.g. Escape to abort.
 * Grabbing the keyboard has the unfortunate side-effect of preventing
 * useful things such as using Alt-Tab to cycle between windows or
 * switching workspaces. Therefore, we just grab the few keys we are
 * interested in. Note that we need to put the grabs on the root window
 * in order for them to still work when the focus is moved to another
 * app/workspace.
 *
 * GDK needs a little help to successfully deliver root key events...
 */

static GdkFilterReturn
root_key_filter (GdkXEvent *xevent,
                 GdkEvent  *event,
                 gpointer   data)
{
  XEvent *ev = (XEvent *)xevent;

  if ((ev->type == KeyPress || ev->type == KeyRelease) &&
      ev->xkey.root == ev->xkey.window)
    ev->xkey.window = (Window)data;

  return GDK_FILTER_CONTINUE;
}

typedef struct {
  gint keysym;
  gint modifiers;
} GrabKey;

static GrabKey grab_keys[] = {
  { XK_Escape, 0 },
  { XK_space, 0 },
  { XK_KP_Space, 0 },
  { XK_Return, 0 },
  { XK_KP_Enter, 0 },
  { XK_Up, 0 },
  { XK_Up, Mod1Mask },
  { XK_Down, 0 },
  { XK_Down, Mod1Mask },
  { XK_Left, 0 },
  { XK_Left, Mod1Mask },
  { XK_Right, 0 },
  { XK_Right, Mod1Mask },
  { XK_KP_Up, 0 },
  { XK_KP_Up, Mod1Mask },
  { XK_KP_Down, 0 },
  { XK_KP_Down, Mod1Mask },
  { XK_KP_Left, 0 },
  { XK_KP_Left, Mod1Mask },
  { XK_KP_Right, 0 },
  { XK_KP_Right, Mod1Mask }
};

static void
grab_dnd_keys (GtkWidget *widget,
               guint32    time)
{
  guint i;
  GdkWindow *window, *root;
  gint keycode;

  window = widget->window;
  root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));

  gdk_error_trap_push ();

  for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
    {
      keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
      XGrabKey (GDK_WINDOW_XDISPLAY (window),
   	        keycode, grab_keys[i].modifiers,
	        GDK_WINDOW_XID (root),
	        FALSE,
	        GrabModeAsync,
	        GrabModeAsync);
    }

  gdk_flush ();
  gdk_error_trap_pop ();

Benjamin Otte's avatar
Benjamin Otte committed
486
  gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
487 488
}

489
static void
490 491 492 493 494 495 496 497 498 499
ungrab_dnd_keys (GtkWidget *widget,
                 guint32    time)
{
  guint i;
  GdkWindow *window, *root;
  gint keycode;

  window = widget->window;
  root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));

Benjamin Otte's avatar
Benjamin Otte committed
500
  gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

  gdk_error_trap_push ();

  for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
    {
      keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
      XUngrabKey (GDK_WINDOW_XDISPLAY (window),
      	          keycode, grab_keys[i].modifiers,
                  GDK_WINDOW_XID (root));
    }

  gdk_flush ();
  gdk_error_trap_pop ();
}

#else

static void
grab_dnd_keys (GtkWidget *widget,
               guint32    time)
{
  gdk_keyboard_grab (widget->window, FALSE, time);
}

static void
ungrab_dnd_keys (GtkWidget *widget,
                 guint32    time)
{
  gdk_display_keyboard_ungrab (gtk_widget_get_display (widget), time);
}

#endif


Owen Taylor's avatar
Owen Taylor committed
535
/***************************************************************
536
 * gtk_drag_release_ipc_widget:
Owen Taylor's avatar
Owen Taylor committed
537
 *     Releases widget retrieved with gtk_drag_get_ipc_widget ()
538 539 540
 *   arguments:
 *     widget: the widget to release.
 *   results:
Owen Taylor's avatar
Owen Taylor committed
541
 ***************************************************************/
542 543 544 545

static void
gtk_drag_release_ipc_widget (GtkWidget *widget)
{
Matthias Clasen's avatar
Matthias Clasen committed
546
  GtkWindow *window = GTK_WINDOW (widget);
547 548 549
  GdkScreen *screen = gtk_widget_get_screen (widget);
  GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
					    "gtk-dnd-ipc-widgets");
550
  ungrab_dnd_keys (widget, GDK_CURRENT_TIME);
Matthias Clasen's avatar
Matthias Clasen committed
551 552
  if (window->group)
    gtk_window_group_remove_window (window->group, window);
553
  drag_widgets = g_slist_prepend (drag_widgets, widget);
554
  g_object_set_data (G_OBJECT (screen),
Matthias Clasen's avatar
Matthias Clasen committed
555
		     I_("gtk-dnd-ipc-widgets"),
556
		     drag_widgets);
557 558
}

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
static guint32
gtk_drag_get_event_time (GdkEvent *event)
{
  guint32 tm = GDK_CURRENT_TIME;
  
  if (event)
    switch (event->type)
      {
      case GDK_MOTION_NOTIFY:
	tm = event->motion.time; break;
      case GDK_BUTTON_PRESS:
      case GDK_2BUTTON_PRESS:
      case GDK_3BUTTON_PRESS:
      case GDK_BUTTON_RELEASE:
	tm = event->button.time; break;
      case GDK_KEY_PRESS:
      case GDK_KEY_RELEASE:
	tm = event->key.time; break;
      case GDK_ENTER_NOTIFY:
      case GDK_LEAVE_NOTIFY:
	tm = event->crossing.time; break;
      case GDK_PROPERTY_NOTIFY:
	tm = event->property.time; break;
      case GDK_SELECTION_CLEAR:
      case GDK_SELECTION_REQUEST:
      case GDK_SELECTION_NOTIFY:
	tm = event->selection.time; break;
      case GDK_PROXIMITY_IN:
      case GDK_PROXIMITY_OUT:
	tm = event->proximity.time; break;
      default:			/* use current time */
	break;
      }
  
  return tm;
}

596 597 598 599 600 601
static void
gtk_drag_get_event_actions (GdkEvent *event, 
			    gint button, 
			    GdkDragAction  actions,
			    GdkDragAction *suggested_action,
			    GdkDragAction *possible_actions)
602
{
603 604
  *suggested_action = 0;
  *possible_actions = 0;
Owen Taylor's avatar
Owen Taylor committed
605

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
  if (event)
    {
      GdkModifierType state = 0;
      
      switch (event->type)
	{
	case GDK_MOTION_NOTIFY:
	  state = event->motion.state;
	  break;
	case GDK_BUTTON_PRESS:
	case GDK_2BUTTON_PRESS:
	case GDK_3BUTTON_PRESS:
	case GDK_BUTTON_RELEASE:
	  state = event->button.state;
	  break;
	case GDK_KEY_PRESS:
	case GDK_KEY_RELEASE:
	  state = event->key.state;
	  break;
Owen Taylor's avatar
Owen Taylor committed
625 626
	case GDK_ENTER_NOTIFY:
	case GDK_LEAVE_NOTIFY:
627 628
	  state = event->crossing.state;
	  break;
Owen Taylor's avatar
Owen Taylor committed
629 630 631
	default:
	  break;
	}
632

Owen Taylor's avatar
Owen Taylor committed
633
      if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
634 635 636 637 638
	{
	  *suggested_action = GDK_ACTION_ASK;
	  *possible_actions = actions;
	}
      else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
639 640
	{
	  if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
641 642 643 644 645 646 647
	    {
	      if (actions & GDK_ACTION_LINK)
		{
		  *suggested_action = GDK_ACTION_LINK;
		  *possible_actions = GDK_ACTION_LINK;
		}
	    }
648
	  else if (state & GDK_CONTROL_MASK)
649 650 651 652 653 654 655 656
	    {
	      if (actions & GDK_ACTION_COPY)
		{
		  *suggested_action = GDK_ACTION_COPY;
		  *possible_actions = GDK_ACTION_COPY;
		}
	      return;
	    }
657
	  else
658 659 660 661 662 663 664 665
	    {
	      if (actions & GDK_ACTION_MOVE)
		{
		  *suggested_action = GDK_ACTION_MOVE;
		  *possible_actions = GDK_ACTION_MOVE;
		}
	      return;
	    }
666 667 668
	}
      else
	{
669
	  *possible_actions = actions;
Owen Taylor's avatar
Owen Taylor committed
670

671 672 673 674
	  if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
	    *suggested_action = GDK_ACTION_ASK;
	  else if (actions & GDK_ACTION_COPY)
	    *suggested_action =  GDK_ACTION_COPY;
675
	  else if (actions & GDK_ACTION_MOVE)
676
	    *suggested_action = GDK_ACTION_MOVE;
677
	  else if (actions & GDK_ACTION_LINK)
678
	    *suggested_action = GDK_ACTION_LINK;
679 680
	}
    }
Owen Taylor's avatar
Owen Taylor committed
681 682 683 684 685 686 687 688 689 690 691
  else
    {
      *possible_actions = actions;
      
      if (actions & GDK_ACTION_COPY)
	*suggested_action =  GDK_ACTION_COPY;
      else if (actions & GDK_ACTION_MOVE)
	*suggested_action = GDK_ACTION_MOVE;
      else if (actions & GDK_ACTION_LINK)
	*suggested_action = GDK_ACTION_LINK;
    }
692 693 694 695 696 697 698 699
}

static gboolean
gtk_drag_can_use_rgba_cursor (GdkDisplay *display, 
			      gint        width,
			      gint        height)
{
  guint max_width, max_height;
700
  
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
  if (!gdk_display_supports_cursor_color (display))
    return FALSE;

  if (!gdk_display_supports_cursor_alpha (display))
    return FALSE;

  gdk_display_get_maximal_cursor_size (display, 
                                       &max_width,
                                       &max_height);
  if (width > max_width || height > max_height)
    {
       /* can't use rgba cursor (too large) */
      return FALSE;
    }

  return TRUE;
717 718
}

Owen Taylor's avatar
Owen Taylor committed
719
static GdkCursor *
720 721 722
gtk_drag_get_cursor (GdkDisplay        *display,
		     GdkDragAction      action,
		     GtkDragSourceInfo *info)
723
{
Matthias Clasen's avatar
Matthias Clasen committed
724
  gint i;
725 726 727 728 729 730 731 732 733 734 735 736 737 738

  /* reconstruct the cursors for each new drag (thus !info),
   * to catch cursor theme changes 
   */ 
  if (!info)
    {
      for (i = 0 ; i < n_drag_cursors - 1; i++)
	if (drag_cursors[i].cursor != NULL)
	  {
	    gdk_cursor_unref (drag_cursors[i].cursor);
	    drag_cursors[i].cursor = NULL;
	  }
    }
 
739 740 741
  for (i = 0 ; i < n_drag_cursors - 1; i++)
    if (drag_cursors[i].action == action)
      break;
742 743 744 745 746

  if (drag_cursors[i].pixbuf == NULL)
    drag_cursors[i].pixbuf = 
      gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);

747 748
  if (drag_cursors[i].cursor != NULL)
    {
749
      if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
750 751 752 753 754
	{
	  gdk_cursor_unref (drag_cursors[i].cursor);
	  drag_cursors[i].cursor = NULL;
	}
    }
755 756 757 758
  
  if (drag_cursors[i].cursor == NULL)
    drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
  
759
  if (drag_cursors[i].cursor == NULL)
760 761 762
    drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);

  if (info && info->icon_pixbuf) 
763
    {
764 765 766 767
      gint cursor_width, cursor_height;
      gint icon_width, icon_height;
      gint width, height;
      GdkPixbuf *cursor_pixbuf, *pixbuf;
768
      gint hot_x, hot_y;
Manish Singh's avatar
Manish Singh committed
769
      gint icon_x, icon_y, ref_x, ref_y;
770 771 772 773 774 775 776 777 778

      if (info->drag_cursors[i] != NULL)
        {
          if (display == gdk_cursor_get_display (info->drag_cursors[i]))
	    return info->drag_cursors[i];
	  
	  gdk_cursor_unref (info->drag_cursors[i]);
	  info->drag_cursors[i] = NULL;
        }
Owen Taylor's avatar
Owen Taylor committed
779

780 781
      icon_x = info->hot_x;
      icon_y = info->hot_y;
Matthias Clasen's avatar
Matthias Clasen committed
782 783 784 785
      icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
      icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);

      hot_x = hot_y = 0;
786 787
      cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
      if (!cursor_pixbuf)
788
	cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
789 790 791 792 793 794 795
      else
	{
	  if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
	    hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
	  
	  if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
	    hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
796

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
#if 0	  
	  /* The code below is an attempt to let cursor themes
	   * determine the attachment of the icon to enable things
	   * like the following:
	   *
	   *    +-----+
           *    |     |
           *    |     ||
           *    +-----+|
           *        ---+
           * 
           * It does not work since Xcursor doesn't allow to attach
           * any additional information to cursors in a retrievable
           * way  (there are comments, but no way to get at them
           * short of searching for the actual cursor file).
           * If this code ever gets used, the icon_window placement
           * must be changed to recognize these placement options
           * as well. Note that this code ignores info->hot_x/y.
           */ 
816 817 818
	  for (j = 0; j < 10; j++)
	    {
	      const gchar *opt;
819
	      gchar key[32];
820
	      gchar **toks;
821
	      GtkAnchorType icon_anchor;
Owen Taylor's avatar
Owen Taylor committed
822

823 824 825 826 827 828 829 830 831 832 833 834 835
	      g_snprintf (key, 32, "comment%d", j);
	      opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
	      if (opt && g_str_has_prefix ("icon-attach:", opt))
		{
		  toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
		  if (g_strv_length (toks) != 3)
		    {
		      g_strfreev (toks);
		      break;
		    }
		  icon_anchor = atoi (toks[0]);
		  icon_x = atoi (toks[1]);
		  icon_y = atoi (toks[2]);
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
		  
		  switch (icon_anchor)
		    {
		    case GTK_ANCHOR_NORTH:
		    case GTK_ANCHOR_CENTER:
		    case GTK_ANCHOR_SOUTH:
		      icon_x += icon_width / 2;
		      break;
		    case GTK_ANCHOR_NORTH_EAST:
		    case GTK_ANCHOR_EAST:
		    case GTK_ANCHOR_SOUTH_EAST:
		      icon_x += icon_width;
		      break;
		    default: ;
		    }
		  
		  switch (icon_anchor)
		    {
		    case GTK_ANCHOR_WEST:
		    case GTK_ANCHOR_CENTER:
		    case GTK_ANCHOR_EAST:
		      icon_y += icon_height / 2;
		      break;
		    case GTK_ANCHOR_SOUTH_WEST:
		    case GTK_ANCHOR_SOUTH:
		    case GTK_ANCHOR_SOUTH_EAST:
		      icon_x += icon_height;
		      break;
		    default: ;
		    }
866 867 868 869 870

		  g_strfreev (toks);
		  break;
		}
	    }
871
#endif
872
	}
Matthias Clasen's avatar
Matthias Clasen committed
873

874 875
      cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
      cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
Matthias Clasen's avatar
Matthias Clasen committed
876
      
877 878 879 880 881 882 883
      ref_x = MAX (hot_x, icon_x);
      ref_y = MAX (hot_y, icon_y);
      width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
      height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
         
      if (gtk_drag_can_use_rgba_cursor (display, width, height))
	{
884 885 886
	  /* Composite cursor and icon so that both hotspots
	   * end up at (ref_x, ref_y)
	   */
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
	  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
				   width, height); 
	  
	  gdk_pixbuf_fill (pixbuf, 0xff000000);
	  
	  gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
				ref_x - icon_x, ref_y - icon_y, 
				icon_width, icon_height,
				ref_x - icon_x, ref_y - icon_y, 
				1.0, 1.0, 
				GDK_INTERP_BILINEAR, 255);
	  
	  gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
				ref_x - hot_x, ref_y - hot_y, 
				cursor_width, cursor_height,
				ref_x - hot_x, ref_y - hot_y,
				1.0, 1.0, 
				GDK_INTERP_BILINEAR, 255);
	  
	  info->drag_cursors[i] = 
907
	    gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
908 909 910 911 912 913 914 915 916 917
	  
	  g_object_unref (pixbuf);
	}
      
      g_object_unref (cursor_pixbuf);
      
      if (info->drag_cursors[i] != NULL)
	return info->drag_cursors[i];
    }
 
918 919 920
  return drag_cursors[i].cursor;
}

921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
static void
gtk_drag_update_cursor (GtkDragSourceInfo *info)
{
  GdkCursor *cursor;
  gint i;

  if (!info->have_grab)
    return;

  for (i = 0 ; i < n_drag_cursors - 1; i++)
    if (info->cursor == drag_cursors[i].cursor ||
	info->cursor == info->drag_cursors[i])
      break;
  
  if (i == n_drag_cursors)
    return;

  cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor), 
939
				drag_cursors[i].action, info);
940 941 942 943 944 945 946 947 948 949 950 951
  
  if (cursor != info->cursor)
    {
      gdk_pointer_grab (info->ipc_widget->window, FALSE,
			GDK_POINTER_MOTION_MASK |
			GDK_BUTTON_RELEASE_MASK,
			NULL,
			cursor, info->grab_time);
      info->cursor = cursor;
    }
}

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
/********************
 * Destination side *
 ********************/

/*************************************************************
 * gtk_drag_get_data:
 *     Get the data for a drag or drop
 *   arguments:
 *     context - drag context
 *     target  - format to retrieve the data in.
 *     time    - timestamp of triggering event.
 *     
 *   results:
 *************************************************************/

void 
gtk_drag_get_data (GtkWidget      *widget,
		   GdkDragContext *context,
		   GdkAtom         target,
		   guint32         time)
{
  GtkWidget *selection_widget;
Owen Taylor's avatar
Owen Taylor committed
974

975 976 977
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
  g_return_if_fail (!context->is_source);
Owen Taylor's avatar
Owen Taylor committed
978

Matthias Clasen's avatar
Matthias Clasen committed
979
  selection_widget = gtk_drag_get_ipc_widget (widget);
Owen Taylor's avatar
Owen Taylor committed
980

Manish Singh's avatar
Manish Singh committed
981 982
  g_object_ref (context);
  g_object_ref (widget);
983
  
984
  g_signal_connect (selection_widget, "selection-received",
Manish Singh's avatar
Manish Singh committed
985
		    G_CALLBACK (gtk_drag_selection_received), widget);
Owen Taylor's avatar
Owen Taylor committed
986

Matthias Clasen's avatar
Matthias Clasen committed
987
  g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
Owen Taylor's avatar
Owen Taylor committed
988

989
  gtk_selection_convert (selection_widget,
Owen Taylor's avatar
Owen Taylor committed
990
			 gdk_drag_get_selection (context),
991 992 993 994
			 target,
			 time);
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

/*************************************************************
 * gtk_drag_get_source_widget:
 *     Get the widget the was the source of this drag, if
 *     the drag originated from this application.
 *   arguments:
 *     context: The drag context for this drag
 *   results:
 *     The source widget, or NULL if the drag originated from
 *     a different application.
 *************************************************************/

Owen Taylor's avatar
Owen Taylor committed
1007
GtkWidget *
1008 1009 1010
gtk_drag_get_source_widget (GdkDragContext *context)
{
  GSList *tmp_list;
Owen Taylor's avatar
Owen Taylor committed
1011

1012 1013 1014
  g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
  g_return_val_if_fail (!context->is_source, NULL);
  
1015 1016 1017 1018 1019 1020 1021 1022
  tmp_list = source_widgets;
  while (tmp_list)
    {
      GtkWidget *ipc_widget = tmp_list->data;
      
      if (ipc_widget->window == context->source_window)
	{
	  GtkDragSourceInfo *info;
Manish Singh's avatar
Manish Singh committed
1023
	  info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
Owen Taylor's avatar
Owen Taylor committed
1024

1025 1026
	  return info ? info->widget : NULL;
	}
Owen Taylor's avatar
Owen Taylor committed
1027

1028 1029
      tmp_list = tmp_list->next;
    }
Owen Taylor's avatar
Owen Taylor committed
1030

1031 1032 1033
  return NULL;
}

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
/*************************************************************
 * gtk_drag_finish:
 *     Notify the drag source that the transfer of data
 *     is complete.
 *   arguments:
 *     context: The drag context for this drag
 *     success: Was the data successfully transferred?
 *     time:    The timestamp to use when notifying the destination.
 *   results:
 *************************************************************/

void 
gtk_drag_finish (GdkDragContext *context,
		 gboolean        success,
1048
		 gboolean        del,
1049 1050 1051
		 guint32         time)
{
  GdkAtom target = GDK_NONE;
Owen Taylor's avatar
Owen Taylor committed
1052

1053 1054
  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
  g_return_if_fail (!context->is_source);
Owen Taylor's avatar
Owen Taylor committed
1055

1056
  if (success && del)
1057
    {
1058
      target = gdk_atom_intern_static_string ("DELETE");
1059 1060 1061
    }
  else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
    {
1062 1063 1064
      target = gdk_atom_intern_static_string (success ? 
					      "XmTRANSFER_SUCCESS" : 
					      "XmTRANSFER_FAILURE");
1065
    }
Owen Taylor's avatar
Owen Taylor committed
1066

1067 1068
  if (target != GDK_NONE)
    {
Matthias Clasen's avatar
Matthias Clasen committed
1069
      GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_drawable_get_screen (context->source_window));
Owen Taylor's avatar
Owen Taylor committed
1070

Manish Singh's avatar
Manish Singh committed
1071
      g_object_ref (context);
1072
      
Matthias Clasen's avatar
Matthias Clasen committed
1073
      g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1074
      g_signal_connect (selection_widget, "selection-received",
Manish Singh's avatar
Manish Singh committed
1075 1076
			G_CALLBACK (gtk_drag_selection_received),
			NULL);
1077 1078
      
      gtk_selection_convert (selection_widget,
Owen Taylor's avatar
Owen Taylor committed
1079
			     gdk_drag_get_selection (context),
1080 1081 1082 1083
			     target,
			     time);
    }
  
1084
  if (!(success && del))
1085 1086 1087 1088
    gdk_drop_finish (context, success, time);
}

/*************************************************************
Owen Taylor's avatar
Owen Taylor committed
1089 1090
 * gtk_drag_highlight_expose:
 *     Callback for expose_event for highlighted widgets.
1091 1092
 *   arguments:
 *     widget:
Owen Taylor's avatar
Owen Taylor committed
1093 1094
 *     event:
 *     data:
1095 1096 1097
 *   results:
 *************************************************************/

Owen Taylor's avatar
Owen Taylor committed
1098 1099 1100 1101
static gboolean
gtk_drag_highlight_expose (GtkWidget      *widget,
			   GdkEventExpose *event,
			   gpointer        data)
1102
{
1103
  gint x, y, width, height;
Owen Taylor's avatar
Owen Taylor committed
1104
  
1105
  if (GTK_WIDGET_DRAWABLE (widget))
1106
    {
1107 1108
      cairo_t *cr;
      
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
      if (GTK_WIDGET_NO_WINDOW (widget))
	{
	  x = widget->allocation.x;
	  y = widget->allocation.y;
	  width = widget->allocation.width;
	  height = widget->allocation.height;
	}
      else
	{
	  x = 0;
	  y = 0;
Manish Singh's avatar
Manish Singh committed
1120
	  gdk_drawable_get_size (widget->window, &width, &height);
1121 1122
	}
      
Manish Singh's avatar
Manish Singh committed
1123 1124
      gtk_paint_shadow (widget->style, widget->window,
		        GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1125
		        &event->area, widget, "dnd",
Manish Singh's avatar
Manish Singh committed
1126
			x, y, width, height);
1127 1128 1129 1130 1131 1132 1133 1134 1135

      cr = gdk_cairo_create (widget->window);
      cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
      cairo_set_line_width (cr, 1.0);
      cairo_rectangle (cr,
		       x + 0.5, y + 0.5,
		       width - 1, height - 1);
      cairo_stroke (cr);
      cairo_destroy (cr);
1136
    }
1137

Owen Taylor's avatar
Owen Taylor committed
1138
  return FALSE;
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
}

/*************************************************************
 * gtk_drag_highlight:
 *     Highlight the given widget in the default manner.
 *   arguments:
 *     widget:
 *   results:
 *************************************************************/

void 
gtk_drag_highlight (GtkWidget  *widget)
{
1152
  g_return_if_fail (GTK_IS_WIDGET (widget));
Manish Singh's avatar
Manish Singh committed
1153

1154
  g_signal_connect_after (widget, "expose-event",
Manish Singh's avatar
Manish Singh committed
1155 1156
			  G_CALLBACK (gtk_drag_highlight_expose),
			  NULL);
Owen Taylor's avatar
Owen Taylor committed
1157

1158
  gtk_widget_queue_draw (widget);
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
}

/*************************************************************
 * gtk_drag_unhighlight:
 *     Refresh the given widget to remove the highlight.
 *   arguments:
 *     widget:
 *   results:
 *************************************************************/

void 
Owen Taylor's avatar
Owen Taylor committed
1170
gtk_drag_unhighlight (GtkWidget *widget)
1171
{
1172
  g_return_if_fail (GTK_IS_WIDGET (widget));
Manish Singh's avatar
Manish Singh committed
1173 1174 1175 1176

  g_signal_handlers_disconnect_by_func (widget,
					gtk_drag_highlight_expose,
					NULL);
1177
  
Manish Singh's avatar
Manish Singh committed
1178
  gtk_widget_queue_draw (widget);
1179 1180
}

Owen Taylor's avatar
Owen Taylor committed
1181 1182 1183 1184 1185 1186 1187 1188 1189
static void
gtk_drag_dest_set_internal (GtkWidget       *widget,
			    GtkDragDestSite *site)
{
  GtkDragDestSite *old_site;
  
  g_return_if_fail (widget != NULL);

  /* HACK, do this in the destroy */
Manish Singh's avatar
Manish Singh committed
1190
  old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
Owen Taylor's avatar
Owen Taylor committed
1191
  if (old_site)
Manish Singh's avatar
Manish Singh committed
1192 1193 1194 1195 1196 1197 1198
    {
      g_signal_handlers_disconnect_by_func (widget,
					    gtk_drag_dest_realized,
					    old_site);
      g_signal_handlers_disconnect_by_func (widget,
					    gtk_drag_dest_hierarchy_changed,
					    old_site);
1199 1200

      site->track_motion = old_site->track_motion;
Manish Singh's avatar
Manish Singh committed
1201
    }
Owen Taylor's avatar
Owen Taylor committed
1202 1203 1204 1205

  if (GTK_WIDGET_REALIZED (widget))
    gtk_drag_dest_realized (widget);

Manish Singh's avatar
Manish Singh committed
1206 1207
  g_signal_connect (widget, "realize",
		    G_CALLBACK (gtk_drag_dest_realized), site);
1208
  g_signal_connect (widget, "hierarchy-changed",
Manish Singh's avatar
Manish Singh committed
1209
		    G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
Owen Taylor's avatar
Owen Taylor committed
1210

Matthias Clasen's avatar
Matthias Clasen committed
1211
  g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
Manish Singh's avatar
Manish Singh committed
1212
			  site, gtk_drag_dest_site_destroy);
Owen Taylor's avatar
Owen Taylor committed
1213 1214
}

1215
/**
1216
 * gtk_drag_dest_set:
1217 1218 1219
 * @widget: a #GtkWidget
 * @flags: which types of default drag behavior to use
 * @targets: a pointer to an array of #GtkTargetEntry<!-- -->s indicating
1220 1221
 * the drop types that this @widget will accept. Later you can access the list
 * with gtk_drag_dest_get_target_list() and gtk_drag_dest_find_target().
1222 1223 1224 1225 1226 1227 1228
 * @n_targets: the number of entries in @targets.
 * @actions: a bitmask of possible actions for a drop onto this @widget.
 *
 * Sets a widget as a potential drop destination, and adds default behaviors.
 *
 * The default behaviors listed in @flags have an effect similar
 * to installing default handlers for the widget's drag-and-drop signals
1229 1230 1231 1232
 * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
 * sufficient to connect to the widget's #GtkWidget::drag-data-received
 * signal to get primitive, but consistent drag-and-drop support.
1233
 *
1234 1235 1236 1237 1238 1239 1240 1241
 * Things become more complicated when you try to preview the dragged data,
 * as described in the documentation for #GtkWidget:drag-motion. The default
 * behaviors described by @flags make some assumptions, that can conflict
 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
 * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
 * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
 * Especially the later is dramatic, when your own #GtkWidget:drag-motion
 * handler calls gtk_drag_get_data() to inspect the dragged data.
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
 *
 * There's no way to set a default action here, you can use the
 * #GtkWidget:drag-motion callback for that. Here's an example which selects
 * the action to use depending on whether the control key is pressed or not:
 * |[
 * static void
 * drag_motion (GtkWidget *widget,
 *              GdkDragContext *context,
 *              gint x,
 *              gint y,
 *              guint time)
 * {
 *   GdkModifierType mask;
 *
 *   gdk_window_get_pointer (gtk_widget_get_window (widget),
 *                           NULL, NULL, &mask);
 *   if (mask & GDK_CONTROL_MASK)
 *     gdk_drag_status (context, GDK_ACTION_COPY, time);
 *   else
 *     gdk_drag_status (context, GDK_ACTION_MOVE, time);
 * }
 * ]|
1264 1265 1266 1267 1268 1269 1270
 */
void
gtk_drag_dest_set (GtkWidget            *widget,
		   GtkDestDefaults       flags,
		   const GtkTargetEntry *targets,
		   gint                  n_targets,
		   GdkDragAction         actions)
1271 1272 1273
{
  GtkDragDestSite *site;
  
1274
  g_return_if_fail (GTK_IS_WIDGET (widget));
Owen Taylor's avatar
Owen Taylor committed
1275

1276
  site = g_new (GtkDragDestSite, 1);
Owen Taylor's avatar
Owen Taylor committed
1277

1278 1279
  site->flags = flags;
  site->have_drag = FALSE;
1280 1281 1282 1283
  if (targets)
    site->target_list = gtk_target_list_new (targets, n_targets);
  else
    site->target_list = NULL;
1284
  site->actions = actions;
1285
  site->do_proxy = FALSE;
1286
  site->proxy_window = NULL;
1287
  site->track_motion = FALSE;
Owen Taylor's avatar
Owen Taylor committed
1288

Owen Taylor's avatar
Owen Taylor committed
1289
  gtk_drag_dest_set_internal (widget, site);
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
}

/*************************************************************
 * gtk_drag_dest_set_proxy:
 *     Set up this widget to proxy drags elsewhere.
 *   arguments:
 *     widget:          
 *     proxy_window:    window to which forward drag events
 *     protocol:        Drag protocol which the dest widget accepts
 *     use_coordinates: If true, send the same coordinates to the
 *                      destination, because it is a embedded 
 *                      subwindow.
 *   results:
 *************************************************************/

void 
gtk_drag_dest_set_proxy (GtkWidget      *widget,
			 GdkWindow      *proxy_window,
			 GdkDragProtocol protocol,
			 gboolean        use_coordinates)
{
  GtkDragDestSite *site;
  
1313 1314
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
Owen Taylor's avatar
Owen Taylor committed
1315

1316
  site = g_new (GtkDragDestSite, 1);
Owen Taylor's avatar
Owen Taylor committed
1317

1318 1319 1320 1321 1322
  site->flags = 0;
  site->have_drag = FALSE;
  site->target_list = NULL;
  site->actions = 0;
  site->proxy_window = proxy_window;
1323
  if (proxy_window)
Manish Singh's avatar
Manish Singh committed
1324
    g_object_ref (proxy_window);
1325
  site->do_proxy = TRUE;
1326 1327
  site->proxy_protocol = protocol;
  site->proxy_coords = use_coordinates;
1328
  site->track_motion = FALSE;
Owen Taylor's avatar
Owen Taylor committed
1329

Owen Taylor's avatar
Owen Taylor committed
1330
  gtk_drag_dest_set_internal (widget, site);
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
}

/*************************************************************
 * gtk_drag_dest_unset
 *     Unregister this widget as a drag target.
 *   arguments:
 *     widget:
 *   results:
 *************************************************************/

void 
Owen Taylor's avatar
Owen Taylor committed
1342
gtk_drag_dest_unset (GtkWidget *widget)
1343
{
1344
  g_return_if_fail (GTK_IS_WIDGET (widget));
Owen Taylor's avatar
Owen Taylor committed
1345

Matthias Clasen's avatar
Matthias Clasen committed
1346
  g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1347 1348
}

Havoc Pennington's avatar
Havoc Pennington committed
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
/**
 * gtk_drag_dest_get_target_list:
 * @widget: a #GtkWidget
 * 
 * Returns the list of targets this widget can accept from
 * drag-and-drop.