gtkdnd.c 86.8 KB
Newer Older
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 28
#include "gdkconfig.h"

29
#include "gdk/gdkkeysyms.h"
30 31

#include "gtkdnd.h"
32
#include "gtkimage.h"
33 34 35
#include "gtkinvisible.h"
#include "gtkmain.h"
#include "gtksignal.h"
36
#include "gtkstock.h"
Owen Taylor's avatar
Owen Taylor committed
37
#include "gtkwindow.h"
38

39
static GSList *source_widgets = NULL;
40 41 42 43 44 45 46 47 48

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
49 50
typedef enum 
{
51 52 53 54 55
  GTK_DRAG_STATUS_DRAG,
  GTK_DRAG_STATUS_WAIT,
  GTK_DRAG_STATUS_DROP
} GtkDragStatus;

Owen Taylor's avatar
Owen Taylor committed
56 57
struct _GtkDragSourceSite 
{
58 59 60
  GdkModifierType    start_button_mask;
  GtkTargetList     *target_list;        /* Targets for drag data */
  GdkDragAction      actions;            /* Possible actions */
61 62 63 64 65 66 67 68 69

  /* Drag icon */
  GtkImageType icon_type;
  union
  {
    GtkImagePixmapData pixmap;
    GtkImagePixbufData pixbuf;
    GtkImageStockData stock;
  } icon_data;
Havoc Pennington's avatar
Havoc Pennington committed
70
  GdkBitmap *icon_mask;
71

72
  GdkColormap       *colormap;	         /* Colormap for drag icon */
Owen Taylor's avatar
Owen Taylor committed
73

74 75 76 77
  /* Stored button press information to detect drag beginning */
  gint               state;
  gint               x, y;
};
Owen Taylor's avatar
Owen Taylor committed
78
  
Owen Taylor's avatar
Owen Taylor committed
79 80
struct _GtkDragSourceInfo 
{
81 82
  GtkWidget         *widget;
  GtkTargetList     *target_list; /* Targets for drag data */
83
  GdkDragAction      possible_actions; /* Actions allowed by source */
84 85 86 87 88 89
  GdkDragContext    *context;	  /* drag context */
  GtkWidget         *icon_window; /* Window for drag */
  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
90

91 92
  GtkDragStatus      status;	  /* drag status */
  GdkEvent          *last_event;  /* motion event waiting for response */
Owen Taylor's avatar
Owen Taylor committed
93

94 95
  gint               start_x, start_y; /* Initial position */
  gint               cur_x, cur_y;     /* Current Position */
Owen Taylor's avatar
Owen Taylor committed
96

97 98 99
  GList             *selections;  /* selections we've claimed */
  
  GtkDragDestInfo   *proxy_dest;  /* Set if this is a proxy drag */
Owen Taylor's avatar
Owen Taylor committed
100

Owen Taylor's avatar
Owen Taylor committed
101 102 103
  guint              drop_timeout;     /* Timeout for aborting drop */
  guint              destroy_icon : 1; /* If true, destroy icon_window
					*/
104 105
};

Owen Taylor's avatar
Owen Taylor committed
106 107
struct _GtkDragDestSite 
{
108 109 110 111 112
  GtkDestDefaults    flags;
  GtkTargetList     *target_list;
  GdkDragAction      actions;
  GdkWindow         *proxy_window;
  GdkDragProtocol    proxy_protocol;
113
  gboolean           do_proxy : 1;
114 115 116
  gboolean           proxy_coords : 1;
  gboolean           have_drag : 1;
};
Owen Taylor's avatar
Owen Taylor committed
117
  
Owen Taylor's avatar
Owen Taylor committed
118 119
struct _GtkDragDestInfo 
{
120 121 122 123 124 125 126 127 128 129 130 131 132
  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 */
  gboolean           dropped : 1;     /* Set after we receive a drop */
  guint32            proxy_drop_time; /* Timestamp for proxied drop */
  gboolean           proxy_drop_wait : 1; /* Set if we are waiting for a
					   * status reply before sending
					   * a proxied drop on.
					   */
  gint               drop_x, drop_y; /* Position of drop */
};

133
#define DROP_ABORT_TIME 300000
134 135 136 137 138 139

#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
140 141
struct _GtkDragAnim 
{
142 143 144 145 146
  GtkDragSourceInfo *info;
  gint step;
  gint n_steps;
};

Owen Taylor's avatar
Owen Taylor committed
147 148
struct _GtkDragFindData 
{
149 150 151 152 153
  gint x;
  gint y;
  GdkDragContext *context;
  GtkDragDestInfo *info;
  gboolean found;
154
  gboolean toplevel;
155 156 157 158 159 160 161
  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
162
enum {
163
  TARGET_MOTIF_SUCCESS = 0x40000000,
164 165 166 167 168 169 170 171 172 173 174 175 176
  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
177 178 179 180 181
static void          gtk_drag_get_event_actions (GdkEvent        *event, 
					         gint             button,
					         GdkDragAction    actions,
					         GdkDragAction   *suggested_action,
					         GdkDragAction   *possible_actions);
182 183 184
static GdkCursor *   gtk_drag_get_cursor         (GdkScreen      *screen,
						  GdkDragAction   action);
static GtkWidget    *gtk_drag_get_ipc_widget     (GdkScreen	 *screen);
Owen Taylor's avatar
Owen Taylor committed
185
static void          gtk_drag_release_ipc_widget (GtkWidget      *widget);
186

Owen Taylor's avatar
Owen Taylor committed
187 188 189
static gboolean      gtk_drag_highlight_expose   (GtkWidget      *widget,
					  	  GdkEventExpose *event,
						  gpointer        data);
190

191 192 193 194 195 196 197 198 199 200 201
static void     gtk_drag_selection_received     (GtkWidget        *widget,
						 GtkSelectionData *selection_data,
						 guint32           time,
						 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
202
						 GtkWidget        *previous_toplevel);
203 204 205 206 207 208 209 210 211 212 213 214 215 216
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
217 218 219 220 221 222

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);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

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,
						gboolean           success,
						guint              time);

static gint gtk_drag_source_event_cb           (GtkWidget         *widget,
						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);
static gint gtk_drag_anim_timeout              (gpointer           data);
static void gtk_drag_remove_icon               (GtkDragSourceInfo *info);
Owen Taylor's avatar
Owen Taylor committed
246
static void gtk_drag_source_info_destroy       (GtkDragSourceInfo *info);
247 248 249 250
static void gtk_drag_update                    (GtkDragSourceInfo *info,
						gint               x_root,
						gint               y_root,
						GdkEvent          *event);
251 252 253
static gint gtk_drag_motion_cb                 (GtkWidget         *widget, 
					        GdkEventMotion    *event, 
					        gpointer           data);
254 255 256
static gint gtk_drag_key_cb                    (GtkWidget         *widget, 
					        GdkEventKey       *event, 
					        gpointer           data);
257 258 259 260 261 262 263 264 265 266 267
static gint gtk_drag_button_release_cb         (GtkWidget         *widget, 
					        GdkEventButton    *event, 
					        gpointer           data);
static gint gtk_drag_abort_timeout             (gpointer           data);

/************************
 * Cursor and Icon data *
 ************************/

#define action_ask_width 16
#define action_ask_height 16
268
static const guchar action_ask_bits[] = {
269 270 271 272 273 274
  0x00, 0x00, 0xfe, 0x7f, 0xfe, 0x1f, 0x06, 0xc0, 0x76, 0xf8, 0xb6, 0xf7, 
  0xd6, 0xec, 0x66, 0xdb, 0x66, 0xdb, 0x96, 0xec, 0x76, 0xf7, 0x76, 0xfb, 
  0xf6, 0xfc, 0x72, 0xfb, 0x7a, 0xfb, 0xf8, 0xfc, };

#define action_ask_mask_width 16
#define action_ask_mask_height 16
275
static const guchar action_ask_mask_bits[] = {
276 277 278 279 280 281
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x8f, 0x07, 0xcf, 0x0f, 
  0xef, 0x1f, 0xff, 0x3c, 0xff, 0x3c, 0x6f, 0x1f, 0x8f, 0x0f, 0x8f, 0x07, 
  0x0f, 0x03, 0x8f, 0x07, 0x87, 0x07, 0x07, 0x03, };

#define action_copy_width 16
#define action_copy_height 16
282
static const guchar action_copy_bits[] = {
283 284 285 286 287 288
  0x00, 0x00, 0xfe, 0x7f, 0xfe, 0x1f, 0x06, 0xc0, 0x76, 0xfb, 0x76, 0xfb, 
  0x76, 0xfb, 0x06, 0x83, 0xf6, 0xbf, 0xf6, 0xbf, 0x06, 0x83, 0x76, 0xfb, 
  0x76, 0xfb, 0x72, 0xfb, 0x7a, 0xf8, 0xf8, 0xff, };

#define action_copy_mask_width 16
#define action_copy_mask_height 16
289
static const guchar action_copy_mask_bits[] = {
290 291 292 293 294 295
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x8f, 0x07, 0x8f, 0x07, 
  0x8f, 0x07, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x8f, 0x07, 
  0x8f, 0x07, 0x8f, 0x07, 0x87, 0x07, 0x07, 0x00, };

#define action_move_width 16
#define action_move_height 16
296
static const guchar action_move_bits[] = {
297 298 299 300 301 302
  0x00, 0x00, 0xfe, 0x7f, 0xfe, 0x1f, 0x06, 0xc0, 0x96, 0xff, 0x26, 0xff, 
  0xc6, 0xf8, 0xd6, 0xe3, 0x96, 0x8f, 0xb6, 0xbf, 0x36, 0xc3, 0x76, 0xfb, 
  0x76, 0xfa, 0xf2, 0xfa, 0xfa, 0xf8, 0xf8, 0xff, };

#define action_move_mask_width 16
#define action_move_mask_height 16
303
static const guchar action_move_mask_bits[] = {
304 305 306 307 308 309
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x6f, 0x00, 0xff, 0x00, 
  0xff, 0x07, 0xef, 0x1f, 0xef, 0x7f, 0xcf, 0x7f, 0xcf, 0x3f, 0x8f, 0x07, 
  0x8f, 0x07, 0x0f, 0x07, 0x07, 0x07, 0x07, 0x00, };

#define action_link_width 16
#define action_link_height 16
310
static const guchar action_link_bits[] = {
311 312 313 314 315 316
  0x00, 0x00, 0xfe, 0x7f, 0xfe, 0x1f, 0x06, 0xc0, 0x36, 0xf8, 0xd6, 0xf7, 
  0x66, 0xec, 0xa6, 0xe8, 0x26, 0xdf, 0xe6, 0xbd, 0xd6, 0xa7, 0xb6, 0xa8, 
  0xb6, 0xb1, 0x72, 0xdf, 0xfa, 0xe0, 0xf8, 0xff, };

#define action_link_mask_width 16
#define action_link_mask_height 16
317
static const guchar action_link_mask_bits[] = {
318 319 320 321 322 323
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xcf, 0x07, 0xef, 0x0f, 
  0xff, 0x1f, 0x7f, 0x1f, 0xff, 0x3f, 0xff, 0x7f, 0xef, 0x7f, 0xcf, 0x77, 
  0xcf, 0x7f, 0x8f, 0x3f, 0x07, 0x1f, 0x07, 0x00, };

#define action_none_width 16
#define action_none_height 16
324
static const guchar action_none_bits[] = {
325 326 327 328 329 330
  0x00, 0x00, 0xfe, 0x7f, 0xfe, 0x1f, 0x06, 0xc0, 0xf6, 0xff, 0xf6, 0xff, 
  0xf6, 0xff, 0xf6, 0xff, 0xf6, 0xff, 0xf6, 0xff, 0xf6, 0xff, 0xf6, 0xff, 
  0xf6, 0xff, 0xf2, 0xff, 0xfa, 0xff, 0xf8, 0xff, };

#define action_none_mask_width 16
#define action_none_mask_height 16
331
static const guchar action_none_mask_bits[] = {
332 333 334 335 336 337 338
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x0f, 0x00, 0x0f, 0x00, 
  0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x00, 
  0x0f, 0x00, 0x0f, 0x00, 0x07, 0x00, 0x07, 0x00, };

#define CURSOR_WIDTH 16
#define CURSOR_HEIGHT 16

Owen Taylor's avatar
Owen Taylor committed
339
static struct {
340
  GdkDragAction action;
Owen Taylor's avatar
Owen Taylor committed
341 342
  const char *bits;
  const char *mask;
343 344 345 346 347 348 349 350 351 352
  GdkCursor    *cursor;
} drag_cursors[] = {
  { GDK_ACTION_DEFAULT, 0 },
  { GDK_ACTION_ASK,   action_ask_bits,  action_ask_mask_bits,  NULL },
  { GDK_ACTION_COPY,  action_copy_bits, action_copy_mask_bits, NULL },
  { GDK_ACTION_MOVE,  action_move_bits, action_move_mask_bits, NULL },
  { GDK_ACTION_LINK,  action_link_bits, action_link_mask_bits, NULL },
  { 0              ,  action_none_bits, action_none_mask_bits, NULL },
};

Owen Taylor's avatar
Owen Taylor committed
353
static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
354 355 356 357 358 359 360 361 362 363 364 365 366 367

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

/*************************************************************
 * gtk_drag_get_ipc_widget:
 *     Return a invisible, off-screen, override-redirect
 *     widget for IPC.
 *   arguments:
 *     
 *   results:
 *************************************************************/

Owen Taylor's avatar
Owen Taylor committed
368
static GtkWidget *
369
gtk_drag_get_ipc_widget (GdkScreen *screen)
370 371
{
  GtkWidget *result;
372 373
  GSList *drag_widgets = g_object_get_data (G_OBJECT (screen), 
					    "gtk-dnd-ipc-widgets");
374 375 376 377 378 379
  
  if (drag_widgets)
    {
      GSList *tmp = drag_widgets;
      result = drag_widgets->data;
      drag_widgets = drag_widgets->next;
380 381 382
      g_object_set_data (G_OBJECT (screen),
			 "gtk-dnd-ipc-widgets",
			 drag_widgets);
383 384 385 386
      g_slist_free_1 (tmp);
    }
  else
    {
387
      result = gtk_invisible_new_for_screen (screen);
388 389
      gtk_widget_show (result);
    }
Owen Taylor's avatar
Owen Taylor committed
390

391 392 393
  return result;
}

Owen Taylor's avatar
Owen Taylor committed
394
/***************************************************************
395
 * gtk_drag_release_ipc_widget:
Owen Taylor's avatar
Owen Taylor committed
396
 *     Releases widget retrieved with gtk_drag_get_ipc_widget ()
397 398 399
 *   arguments:
 *     widget: the widget to release.
 *   results:
Owen Taylor's avatar
Owen Taylor committed
400
 ***************************************************************/
401 402 403 404

static void
gtk_drag_release_ipc_widget (GtkWidget *widget)
{
405 406 407
  GdkScreen *screen = gtk_widget_get_screen (widget);
  GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
					    "gtk-dnd-ipc-widgets");
408
  drag_widgets = g_slist_prepend (drag_widgets, widget);
409 410 411
  g_object_set_data (G_OBJECT (screen),
		     "gtk-dnd-ipc-widgets",
		     drag_widgets);
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
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;
}

451 452 453 454 455 456
static void
gtk_drag_get_event_actions (GdkEvent *event, 
			    gint button, 
			    GdkDragAction  actions,
			    GdkDragAction *suggested_action,
			    GdkDragAction *possible_actions)
457
{
458 459
  *suggested_action = 0;
  *possible_actions = 0;
Owen Taylor's avatar
Owen Taylor committed
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  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
480 481
	case GDK_ENTER_NOTIFY:
	case GDK_LEAVE_NOTIFY:
482 483
	  state = event->crossing.state;
	  break;
Owen Taylor's avatar
Owen Taylor committed
484 485 486
	default:
	  break;
	}
487

Owen Taylor's avatar
Owen Taylor committed
488
      if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
489 490 491 492 493
	{
	  *suggested_action = GDK_ACTION_ASK;
	  *possible_actions = actions;
	}
      else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
494 495
	{
	  if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
496 497 498 499 500 501 502
	    {
	      if (actions & GDK_ACTION_LINK)
		{
		  *suggested_action = GDK_ACTION_LINK;
		  *possible_actions = GDK_ACTION_LINK;
		}
	    }
503
	  else if (state & GDK_CONTROL_MASK)
504 505 506 507 508 509 510 511
	    {
	      if (actions & GDK_ACTION_COPY)
		{
		  *suggested_action = GDK_ACTION_COPY;
		  *possible_actions = GDK_ACTION_COPY;
		}
	      return;
	    }
512
	  else
513 514 515 516 517 518 519 520
	    {
	      if (actions & GDK_ACTION_MOVE)
		{
		  *suggested_action = GDK_ACTION_MOVE;
		  *possible_actions = GDK_ACTION_MOVE;
		}
	      return;
	    }
521 522 523
	}
      else
	{
524
	  *possible_actions = actions;
Owen Taylor's avatar
Owen Taylor committed
525

526 527 528 529
	  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;
530
	  else if (actions & GDK_ACTION_MOVE)
531
	    *suggested_action = GDK_ACTION_MOVE;
532
	  else if (actions & GDK_ACTION_LINK)
533
	    *suggested_action = GDK_ACTION_LINK;
534 535
	}
    }
Owen Taylor's avatar
Owen Taylor committed
536 537 538 539 540 541 542 543 544 545 546
  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;
    }
547
  
548
  return;
549 550
}

Owen Taylor's avatar
Owen Taylor committed
551
static GdkCursor *
552 553
gtk_drag_get_cursor (GdkScreen    *screen,
		     GdkDragAction action)
554 555 556 557 558 559
{
  gint i;
  
  for (i = 0 ; i < n_drag_cursors - 1; i++)
    if (drag_cursors[i].action == action)
      break;
560 561 562 563 564 565 566 567
  if (drag_cursors[i].cursor != NULL)
    {
      if (screen != gdk_cursor_get_screen (drag_cursors[i].cursor))
	{
	  gdk_cursor_unref (drag_cursors[i].cursor);
	  drag_cursors[i].cursor = NULL;
	}
    }
Owen Taylor's avatar
Owen Taylor committed
568

569 570
  if (drag_cursors[i].cursor == NULL)
    {
571
      GdkColormap *colormap;
572
      GdkColor fg, bg;
Owen Taylor's avatar
Owen Taylor committed
573

574
      GdkPixmap *pixmap = 
575 576 577
	gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
				     drag_cursors[i].bits, CURSOR_WIDTH, CURSOR_HEIGHT);

578
      GdkPixmap *mask = 
579 580
	gdk_bitmap_create_from_data (gdk_screen_get_root_window (screen),
				     drag_cursors[i].mask, CURSOR_WIDTH, CURSOR_HEIGHT);
Owen Taylor's avatar
Owen Taylor committed
581

582 583 584
      colormap = gdk_screen_get_system_colormap (screen);
      gdk_color_white (colormap, &bg);
      gdk_color_black (colormap, &fg);
585 586
      
      drag_cursors[i].cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, 0, 0);
Owen Taylor's avatar
Owen Taylor committed
587

588 589 590
      gdk_pixmap_unref (pixmap);
      gdk_pixmap_unref (mask);
    }
Owen Taylor's avatar
Owen Taylor committed
591

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
  return drag_cursors[i].cursor;
}

/********************
 * 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
617

618 619
  g_return_if_fail (widget != NULL);
  g_return_if_fail (context != NULL);
Owen Taylor's avatar
Owen Taylor committed
620

621
  selection_widget = gtk_drag_get_ipc_widget (gtk_widget_get_screen (widget));
Owen Taylor's avatar
Owen Taylor committed
622

623 624 625 626 627
  gdk_drag_context_ref (context);
  gtk_widget_ref (widget);
  
  gtk_signal_connect (GTK_OBJECT (selection_widget), "selection_received",
		      GTK_SIGNAL_FUNC (gtk_drag_selection_received), widget);
Owen Taylor's avatar
Owen Taylor committed
628

629
  gtk_object_set_data (GTK_OBJECT (selection_widget), "drag-context", context);
Owen Taylor's avatar
Owen Taylor committed
630

631
  gtk_selection_convert (selection_widget,
Owen Taylor's avatar
Owen Taylor committed
632
			 gdk_drag_get_selection (context),
633 634 635 636
			 target,
			 time);
}

637 638 639 640 641 642 643 644 645 646 647 648

/*************************************************************
 * 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
649
GtkWidget *
650 651 652
gtk_drag_get_source_widget (GdkDragContext *context)
{
  GSList *tmp_list;
Owen Taylor's avatar
Owen Taylor committed
653

654 655 656 657 658 659 660 661 662
  tmp_list = source_widgets;
  while (tmp_list)
    {
      GtkWidget *ipc_widget = tmp_list->data;
      
      if (ipc_widget->window == context->source_window)
	{
	  GtkDragSourceInfo *info;
	  info = gtk_object_get_data (GTK_OBJECT (ipc_widget), "gtk-info");
Owen Taylor's avatar
Owen Taylor committed
663

664 665
	  return info ? info->widget : NULL;
	}
Owen Taylor's avatar
Owen Taylor committed
666

667 668
      tmp_list = tmp_list->next;
    }
Owen Taylor's avatar
Owen Taylor committed
669

670 671 672
  return NULL;
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686
/*************************************************************
 * 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,
687
		 gboolean        del,
688 689 690
		 guint32         time)
{
  GdkAtom target = GDK_NONE;
Owen Taylor's avatar
Owen Taylor committed
691

692
  g_return_if_fail (context != NULL);
Owen Taylor's avatar
Owen Taylor committed
693

694
  if (success && del)
695 696 697 698 699 700
    {
      target = gdk_atom_intern ("DELETE", FALSE);
    }
  else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
    {
      target = gdk_atom_intern (success ? 
Owen Taylor's avatar
Owen Taylor committed
701 702
				  "XmTRANSFER_SUCCESS" : 
				  "XmTRANSFER_FAILURE",
703 704
				FALSE);
    }
Owen Taylor's avatar
Owen Taylor committed
705

706 707
  if (target != GDK_NONE)
    {
708
      GtkWidget *selection_widget = gtk_drag_get_ipc_widget (gdk_drawable_get_screen (context->source_window));
Owen Taylor's avatar
Owen Taylor committed
709

710 711 712 713 714 715 716 717
      gdk_drag_context_ref (context);
      
      gtk_object_set_data (GTK_OBJECT (selection_widget), "drag-context", context);
      gtk_signal_connect (GTK_OBJECT (selection_widget), "selection_received",
			  GTK_SIGNAL_FUNC (gtk_drag_selection_received),
			  NULL);
      
      gtk_selection_convert (selection_widget,
Owen Taylor's avatar
Owen Taylor committed
718
			     gdk_drag_get_selection (context),
719 720 721 722
			     target,
			     time);
    }
  
723
  if (!(success && del))
724 725 726 727
    gdk_drop_finish (context, success, time);
}

/*************************************************************
Owen Taylor's avatar
Owen Taylor committed
728 729
 * gtk_drag_highlight_expose:
 *     Callback for expose_event for highlighted widgets.
730 731
 *   arguments:
 *     widget:
Owen Taylor's avatar
Owen Taylor committed
732 733
 *     event:
 *     data:
734 735 736
 *   results:
 *************************************************************/

Owen Taylor's avatar
Owen Taylor committed
737 738 739 740
static gboolean
gtk_drag_highlight_expose (GtkWidget      *widget,
			   GdkEventExpose *event,
			   gpointer        data)
741
{
742
  gint x, y, width, height;
Owen Taylor's avatar
Owen Taylor committed
743
  
744
  if (GTK_WIDGET_DRAWABLE (widget))
745
    {
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
      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;
	  gdk_window_get_size (widget->window, &width, &height);
	}
      
      gtk_draw_shadow (widget->style, widget->window,
		       GTK_STATE_NORMAL, GTK_SHADOW_OUT,
		       x, y, width, height);
      
      gdk_draw_rectangle (widget->window,
			  widget->style->black_gc,
			  FALSE,
			  x, y, width - 1, height - 1);
768
    }
769

Owen Taylor's avatar
Owen Taylor committed
770
  return FALSE;
771 772 773 774 775 776 777 778 779 780 781 782 783
}

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

void 
gtk_drag_highlight (GtkWidget  *widget)
{
784 785 786
  gtk_signal_connect_after (GTK_OBJECT (widget), "expose_event",
			    GTK_SIGNAL_FUNC (gtk_drag_highlight_expose),
			    NULL);
Owen Taylor's avatar
Owen Taylor committed
787

788
  gtk_widget_queue_draw (widget);
789 790 791 792 793 794 795 796 797 798 799
}

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

void 
Owen Taylor's avatar
Owen Taylor committed
800
gtk_drag_unhighlight (GtkWidget *widget)
801 802
{
  g_return_if_fail (widget != NULL);
Owen Taylor's avatar
Owen Taylor committed
803

804 805 806 807 808
  gtk_signal_disconnect_by_func (GTK_OBJECT (widget),
				 GTK_SIGNAL_FUNC (gtk_drag_highlight_expose),
				 NULL);
  
  gtk_widget_queue_clear (widget);
809 810
}

Owen Taylor's avatar
Owen Taylor committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
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 */
  old_site = gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest");
  if (old_site)
    gtk_signal_disconnect_by_data (GTK_OBJECT (widget), old_site);

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

  gtk_signal_connect (GTK_OBJECT (widget), "realize",
		      GTK_SIGNAL_FUNC (gtk_drag_dest_realized), site);
829 830
  gtk_signal_connect (GTK_OBJECT (widget), "hierarchy_changed",
		      GTK_SIGNAL_FUNC (gtk_drag_dest_hierarchy_changed), site);
Owen Taylor's avatar
Owen Taylor committed
831 832 833 834 835 836

  gtk_object_set_data_full (GTK_OBJECT (widget), "gtk-drag-dest",
			    site, gtk_drag_dest_site_destroy);
}
			    

837 838 839 840 841 842 843 844 845 846 847 848 849
/*************************************************************
 * gtk_drag_dest_set:
 *     Register a drop site, and possibly add default behaviors.
 *   arguments:
 *     widget:    
 *     flags:     Which types of default drag behavior to use
 *     targets:   Table of targets that can be accepted
 *     n_targets: Number of of entries in targets
 *     actions:   
 *   results:
 *************************************************************/

void 
850 851 852 853 854
gtk_drag_dest_set   (GtkWidget            *widget,
		     GtkDestDefaults       flags,
		     const GtkTargetEntry *targets,
		     gint                  n_targets,
		     GdkDragAction         actions)
855 856 857 858
{
  GtkDragDestSite *site;
  
  g_return_if_fail (widget != NULL);
Owen Taylor's avatar
Owen Taylor committed
859

860
  site = g_new (GtkDragDestSite, 1);
Owen Taylor's avatar
Owen Taylor committed
861

862 863 864 865 866 867
  site->flags = flags;
  site->have_drag = FALSE;
  if (targets)
    site->target_list = gtk_target_list_new (targets, n_targets);
  else
    site->target_list = NULL;
Owen Taylor's avatar
Owen Taylor committed
868

869
  site->actions = actions;
870
  site->do_proxy = FALSE;
Owen Taylor's avatar
Owen Taylor committed
871

Owen Taylor's avatar
Owen Taylor committed
872
  gtk_drag_dest_set_internal (widget, site);
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
}

/*************************************************************
 * 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;
  
  g_return_if_fail (widget != NULL);
Owen Taylor's avatar
Owen Taylor committed
897

898
  site = g_new (GtkDragDestSite, 1);
Owen Taylor's avatar
Owen Taylor committed
899

900 901 902 903 904
  site->flags = 0;
  site->have_drag = FALSE;
  site->target_list = NULL;
  site->actions = 0;
  site->proxy_window = proxy_window;
905 906 907
  if (proxy_window)
    gdk_window_ref (proxy_window);
  site->do_proxy = TRUE;
908 909
  site->proxy_protocol = protocol;
  site->proxy_coords = use_coordinates;
Owen Taylor's avatar
Owen Taylor committed
910

Owen Taylor's avatar
Owen Taylor committed
911
  gtk_drag_dest_set_internal (widget, site);
912 913 914 915 916 917 918 919 920 921 922
}

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

void 
Owen Taylor's avatar
Owen Taylor committed
923
gtk_drag_dest_unset (GtkWidget *widget)
924 925
{
  g_return_if_fail (widget != NULL);
Owen Taylor's avatar
Owen Taylor committed
926

927 928 929
  gtk_object_set_data (GTK_OBJECT (widget), "gtk-drag-dest", NULL);
}

Havoc Pennington's avatar
Havoc Pennington committed
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
/**
 * gtk_drag_dest_get_target_list:
 * @widget: a #GtkWidget
 * 
 * Returns the list of targets this widget can accept from
 * drag-and-drop.
 * 
 * Return value: the #GtkTargetList, or %NULL if none
 **/
GtkTargetList*
gtk_drag_dest_get_target_list (GtkWidget *widget)
{
  GtkDragDestSite *site;
  
  site = gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest");

  return site ? site->target_list : NULL;  
}

/**
 * gtk_drag_dest_set_target_list:
 * @widget: a #GtkWidget that's a drag destination
 * @target_list: list of droppable targets, or %NULL for none
 * 
 * Sets the target types that this widget can accept from drag-and-drop.
 * The widget must first be made into a drag destination with
 * gtk_drag_dest_set().
 **/
void
gtk_drag_dest_set_target_list (GtkWidget      *widget,
                               GtkTargetList  *target_list)
{
  GtkDragDestSite *site;
  
  site = gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest");
  
  if (site == NULL)
    {
      g_warning ("can't set a target list on a widget until you've called gtk_drag_dest_set() to make the widget into a drag destination");
      return;
    }

  if (target_list)
973
    gtk_target_list_ref (target_list);
Havoc Pennington's avatar
Havoc Pennington committed
974 975 976 977 978 979 980 981
  
  if (site->target_list)
    gtk_target_list_unref (site->target_list);

  site->target_list = target_list;
}


982
/*************************************************************
983
 * _gtk_drag_dest_handle_event:
984 985 986 987 988 989 990 991 992 993
 *     Called from widget event handling code on Drag events
 *     for destinations.
 *
 *   arguments:
 *     toplevel: Toplevel widget that received the event
 *     event:
 *   results:
 *************************************************************/

void
994
_gtk_drag_dest_handle_event (GtkWidget *toplevel,
995 996 997 998
			    GdkEvent  *event)
{
  GtkDragDestInfo *info;
  GdkDragContext *context;
Owen Taylor's avatar
Owen Taylor committed
999

1000 1001
  g_return_if_fail (toplevel != NULL);
  g_return_if_fail (event != NULL);
Owen Taylor's avatar
Owen Taylor committed
1002

1003
  context = event->dnd.context;
Owen Taylor's avatar
Owen Taylor committed
1004

Owen Taylor's avatar
Owen Taylor committed
1005
  info = gtk_drag_get_dest_info (context, TRUE);
Owen Taylor's avatar
Owen Taylor committed
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  /* Find the widget for the event */
  switch (event->type)
    {
    case GDK_DRAG_ENTER:
      break;
      
    case GDK_DRAG_LEAVE:
      if (info->widget)
	{
	  gtk_drag_dest_leave (info->widget, context, event->dnd.time);
	  info->widget = NULL;
	}
      break;
      
    case GDK_DRAG_MOTION:
    case GDK_DROP_START:
Owen Taylor's avatar
Owen Taylor committed
1023 1024 1025 1026 1027
      {
	GtkDragFindData data;
	gint tx, ty;

	if (event->type == GDK_DROP_START)
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
	  {
	    info->dropped = TRUE;
	    /* We send a leave here so that the widget unhighlights
	     * properly.
	     */
	    if (info->widget)
	      {
		gtk_drag_dest_leave (info->widget, context, event->dnd.time);
		info->widget = NULL;
	      }
	  }
Owen Taylor's avatar
Owen Taylor committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

	gdk_window_get_origin (toplevel->window, &tx, &ty);

	data.x = event->dnd.x_root - tx;
	data.y = event->dnd.y_root - ty;
 	data.context = context;
	data.info = info;
	data.found = FALSE;
	data.toplevel = TRUE;
	data.callback = (event->type == GDK_DRAG_MOTION) ?
	  gtk_drag_dest_motion : gtk_drag_dest_drop;
	data.time = event->dnd.time;
	
	gtk_drag_find_widget (toplevel, &data);

1054
	if (info->widget && !data.found)
Owen Taylor's avatar
Owen Taylor committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	  {
	    gtk_drag_dest_leave (info->widget, context, event->dnd.time);
	    info->widget = NULL;
	  }
	
	/* Send a reply.
	 */
	if (event->type == GDK_DRAG_MOTION)
	  {
	    if (!data.found)
	      gdk_drag_status (context, 0, event->dnd.time);
	  }
1067
	else if (event->type == GDK_DROP_START && !info->proxy_source)
Owen Taylor's avatar
Owen Taylor committed
1068 1069 1070 1071 1072 1073 1074 1075
	  {
	    gdk_drop_reply (context, data.found, event->dnd.time);
            if ((context->protocol == GDK_DRAG_PROTO_MOTIF) && !data.found)
	      gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
	  }
      }
      break;

1076
    default:
Owen Taylor's avatar
Owen Taylor committed
1077
      g_assert_not_reached ();
1078 1079 1080
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
1081
/**
1082
 * gtk_drag_dest_find_target:
Havoc Pennington's avatar
Havoc Pennington committed
1083 1084
 * @widget: drag destination widget
 * @context: drag context
1085 1086
 * @target_list: list of droppable targets, or %NULL to use
 *    gtk_drag_dest_get_target_list (@widget).
Havoc Pennington's avatar
Havoc Pennington committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
 * 
 * Looks for a match between @context->targets and the
 * @dest_target_list, returning the first matching target, otherwise
 * returning %GDK_NONE. @dest_target_list should usually be the return
 * value from gtk_drag_dest_get_target_list(), but some widgets may
 * have different valid targets for different parts of the widget; in
 * that case, they will have to implement a drag_motion handler that
 * passes the correct target list to this function.
 * 
 * Return value: first target that the source offers and the dest can accept, or %GDK_NONE
 **/
GdkAtom
gtk_drag_dest_find_target (GtkWidget      *widget,
                           GdkDragContext *context,
1101
                           GtkTargetList  *target_list)
1102 1103 1104
{
  GList *tmp_target;
  GList *tmp_source = NULL;
1105
  GtkWidget *source_widget;
Owen Taylor's avatar
Owen Taylor committed
1106

1107 1108 1109 1110 1111 1112 1113 1114
  g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
  g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);

  source_widget = gtk_drag_get_source_widget (context);

  if (target_list == NULL)
    target_list = gtk_drag_dest_get_target_list (widget);
  
1115
  if (target_list == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
1116 1117
    return GDK_NONE;
  
1118
  tmp_target = target_list->list;
1119 1120 1121 1122 1123 1124 1125
  while (tmp_target)
    {
      GtkTargetPair *pair = tmp_target->data;
      tmp_source = context->targets;
      while (tmp_source)
	{
	  if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1126 1127 1128 1129 1130 1131 1132
	    {
	      if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
		  (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
		return pair->target;
	      else
		break;
	    }
1133 1134 1135 1136
	  tmp_source = tmp_source->next;
	}
      tmp_target = tmp_target->next;
    }
Owen Taylor's avatar
Owen Taylor committed
1137

1138 1139 1140 1141
  return GDK_NONE;
}

static void
Owen Taylor's avatar
Owen Taylor committed
1142 1143 1144 1145
gtk_drag_selection_received (GtkWidget        *widget,
			     GtkSelectionData *selection_data,
			     guint32           time,
			     gpointer          data)
1146 1147 1148 1149
{
  GdkDragContext *context;
  GtkDragDestInfo *info;
  GtkWidget *drop_widget;
Owen Taylor's avatar
Owen Taylor committed
1150

1151
  drop_widget = data;
Owen Taylor's avatar
Owen Taylor committed
1152

1153
  context = gtk_object_get_data (GTK_OBJECT (widget), "drag-context");
Owen Taylor's avatar
Owen Taylor committed
1154
  info = gtk_drag_get_dest_info (context, FALSE);
Owen Taylor's avatar
Owen Taylor committed
1155

1156 1157 1158 1159 1160 1161 1162 1163
  if (info->proxy_data && 
      info->proxy_data->target == selection_data->target)
    {
      gtk_selection_data_set (info->proxy_data,
			      selection_data->type,
			      selection_data->format,
			      selection_data->data,
			      selection_data->length);
Owen Taylor's avatar
Owen Taylor committed
1164
      gtk_main_quit ();
1165 1166
      return;
    }
Owen Taylor's avatar
Owen Taylor committed
1167

1168 1169 1170 1171 1172
  if (selection_data->target == gdk_atom_intern ("DELETE", FALSE))
    {
      gtk_drag_finish (context, TRUE, FALSE, time);
    }
  else if ((selection_data->target == gdk_atom_intern ("XmTRANSFER_SUCCESS", FALSE)) ||
1173
	   (selection_data->target == gdk_atom_intern ("XmTRANSFER_FAILURE", FALSE)))
1174 1175 1176 1177 1178 1179
    {
      /* Do nothing */
    }
  else
    {
      GtkDragDestSite *site;
Owen Taylor's avatar
Owen Taylor committed
1180

1181
      site = gtk_object_get_data (GTK_OBJECT (drop_widget), "gtk-drag-dest");
Owen Taylor's avatar
Owen Taylor committed
1182

1183
      if (site && site->target_list)
1184 1185
	{
	  guint target_info;
Owen Taylor's avatar
Owen Taylor committed
1186

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	  if (gtk_target_list_find (site->target_list, 
				    selection_data->target,
				    &target_info))
	    {
	      if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
		  selection_data->length >= 0)
		gtk_signal_emit_by_name (GTK_OBJECT (drop_widget), 
					 "drag_data_received",
					 context, info->drop_x, info->drop_y,
					 selection_data, 
					 target_info, time);
	    }
	}
      else
	{
	  gtk_signal_emit_by_name (GTK_OBJECT (drop_widget), 
				   "drag_data_received",
				   context, info->drop_x, info->drop_y,
				   selection_data, 0, time);
	}
      
1208
      if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1209
	{
Owen Taylor's avatar
Owen Taylor committed
1210

1211 1212 1213 1214 1215 1216 1217 1218
	  gtk_drag_finish (context, 
			   (selection_data->length >= 0),
			   (context->action == GDK_ACTION_MOVE),
			   time);
	}
      
      gtk_widget_unref (drop_widget);
    }
Owen Taylor's avatar
Owen Taylor committed
1219

1220 1221 1222 1223 1224 1225
  gtk_signal_disconnect_by_func (GTK_OBJECT (widget), 
				 GTK_SIGNAL_FUNC (gtk_drag_selection_received),
				 data);
  
  gtk_object_set_data (GTK_OBJECT (widget), "drag-context", NULL);
  gdk_drag_context_unref (context);
Owen Taylor's avatar
Owen Taylor committed
1226

1227 1228 1229
  gtk_drag_release_ipc_widget (widget);
}

1230 1231 1232 1233 1234 1235 1236 1237 1238
static void
prepend_and_ref_widget (GtkWidget *widget,
			gpointer   data)
{
  GSList **slist_p = data;

  *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
}

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
/*************************************************************
 * gtk_drag_find_widget:
 *     Recursive callback used to locate widgets for 
 *     DRAG_MOTION and DROP_START events.
 *   arguments:
 *     
 *   results:
 *************************************************************/

static void
gtk_drag_find_widget (GtkWidget       *widget,
		      GtkDragFindData *data)
{
  GtkAllocation new_allocation;
1253 1254
  gint allocation_to_window_x = 0;
  gint allocation_to_window_y = 0;
1255 1256
  gint x_offset = 0;
  gint y_offset = 0;
Owen Taylor's avatar
Owen Taylor committed
1257

1258
  if (data->found || !GTK_WIDGET_MAPPED (widget))
1259
    return;
Owen Taylor's avatar
Owen Taylor committed
1260

1261 1262 1263 1264 1265
  /* Note that in the following code, we only count the
   * position as being inside a WINDOW widget if it is inside
   * widget->window; points that are outside of widget->window
   * but within the allocation are not counted. This is consistent
   * with the way we highlight drag targets.
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
   *
   * data->x,y are relative to widget->parent->window (if
   * widget is not a toplevel, widget->window otherwise).
   * We compute the allocation of widget in the same coordinates,
   * clipping to widget->window, and all intermediate
   * windows. If data->x,y is inside that, then we translate
   * our coordinates to be relative to widget->window and
   * recurse.
   */  
  new_allocation = widget->allocation;

1277 1278
  if (widget->parent)
    {
1279
      gint tx, ty;
1280
      GdkWindow *window = widget->window;
1281

1282 1283
      /* Compute the offset from allocation-relative to
       * window-relative coordinates.
1284
       */
1285 1286 1287
      allocation_to_window_x = widget->allocation.x;
      allocation_to_window_y = widget->allocation.y;

1288
      if (!GTK_WIDGET_NO_WINDOW (widget))
1289
	{
1290 1291 1292
	  /* The allocation is relative to the parent window for
	   * window widgets, not to widget->window.
	   */
1293 1294
          gdk_window_get_position (window, &tx, &ty);
	  
1295 1296
          allocation_to_window_x -= tx;
          allocation_to_window_y -= ty;
1297
	}
1298

1299 1300 1301
      new_allocation.x = 0 + allocation_to_window_x;
      new_allocation.y = 0 + allocation_to_window_y;
      
1302
      while (window && window != widget->parent->window)
1303 1304 1305 1306
	{
	  GdkRectangle window_rect = { 0, 0, 0, 0 };
	  
	  gdk_window_get_size (window, &window_rect.width, &window_rect.height);
Owen Taylor's avatar
Owen Taylor committed
1307

1308
	  gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
Owen Taylor's avatar
Owen Taylor committed
1309

1310 1311 1312 1313 1314 1315 1316 1317
	  gdk_window_get_position (window, &tx, &ty);
	  new_allocation.x += tx;
	  x_offset += tx;
	  new_allocation.y += ty;
	  y_offset += ty;
	  
	  window = gdk_window_get_parent (window);
	}
1318 1319 1320

      if (!window)		/* Window and widget heirarchies didn't match. */
	return;
1321
    }
Owen Taylor's avatar
Owen Taylor committed
1322

1323 1324 1325 1326
  if (data->toplevel ||
      ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
       (data->x < new_allocation.x + new_allocation.width) && 
       (data->y < new_allocation.y + new_allocation.height)))
1327 1328 1329 1330 1331 1332 1333
    {
      /* First, check if the drag is in a valid drop site in
       * one of our children 
       */
      if (GTK_IS_CONTAINER (widget))
	{
	  GtkDragFindData new_data = *data;
1334 1335
	  GSList *children = NULL;
	  GSList *tmp_list;
1336 1337 1338 1339
	  
	  new_data.x -= x_offset;
	  new_data.y -= y_offset;
	  new_data.found = FALSE;
1340
	  new_data.toplevel = FALSE;
1341
	  
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	  /* need to reference children temporarily in case the
	   * ::drag_motion/::drag_drop callbacks change the widget heirarchy.
	   */
	  gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
	  for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
	    {
	      if (!new_data.found && GTK_WIDGET_DRAWABLE (tmp_list->data))
		gtk_drag_find_widget (tmp_list->data, &new_data);
	      gtk_widget_unref (tmp_list->data);
	    }
	  g_slist_free (children);
1353 1354 1355
	  
	  data->found = new_data.found;
	}
Owen Taylor's avatar
Owen Taylor committed
1356

1357 1358 1359 1360 1361 1362 1363 1364 1365
      /* If not, and this widget is registered as a drop site, check to
       * emit "drag_motion" to check if we are actually in
       * a drop site.
       */
      if (!data->found &&
	  gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest"))
	{
	  data->found = data->callback (widget,
					data->context,
1366 1367
					data->x - x_offset - allocation_to_window_x,
					data->y - y_offset - allocation_to_window_y,
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
					data->time);
	  /* If so, send a "drag_leave" to the last widget */
	  if (data->found)
	    {
	      if (data->info->widget && data->info->widget != widget)
		{
		  gtk_drag_dest_leave (data->info->widget, data->context, data->time);
		}
	      data->info->widget = widget;
	    }
	}
    }
}

static void
Owen Taylor's avatar
Owen Taylor committed
1383
gtk_drag_proxy_begin (GtkWidget       *widget, 
Owen Taylor's avatar
Owen Taylor committed
1384 1385
		      GtkDragDestInfo *dest_info,
		      guint32          time)
1386 1387 1388
{
  GtkDragSourceInfo *source_info;
  GList *tmp_list;
Owen Taylor's avatar
Owen Taylor committed
1389 1390 1391 1392 1393 1394 1395 1396 1397
  GdkDragContext *context;
  GtkWidget *ipc_widget;

  if (dest_info->proxy_source)
    {
      gdk_drag_abort (dest_info->proxy_source->context, time);
      gtk_drag_source_info_destroy (dest_info->proxy_source);
      dest_info->proxy_source = NULL;
    }
1398
  
1399
  ipc_widget = gtk_drag_get_ipc_widget (gtk_widget_get_screen (widget));
Owen Taylor's avatar
Owen Taylor committed
1400 1401 1402 1403 1404 1405 1406
  context = gdk_drag_begin (ipc_widget->window,
			    dest_info->context->targets);

  source_info = gtk_drag_get_source_info (context, TRUE);

  source_info->ipc_widget = ipc_widget;
  source_info->widget = gtk_widget_ref (widget);
Owen Taylor's avatar
Owen Taylor committed
1407