gtkdnd.c 67.8 KB
Newer Older
1
/* GTK - The GIMP Toolkit
2
 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gdk/gdkx.h"

#include "gtkdnd.h"
#include "gtkinvisible.h"
#include "gtkmain.h"
#include "gtksignal.h"
Owen Taylor's avatar
Owen Taylor committed
26
#include "gtkwindow.h"
27 28

static GSList *drag_widgets = NULL;
29
static GSList *source_widgets = NULL;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

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


typedef enum {
  GTK_DRAG_STATUS_DRAG,
  GTK_DRAG_STATUS_WAIT,
  GTK_DRAG_STATUS_DROP
} GtkDragStatus;

struct _GtkDragSourceSite {
  GdkModifierType    start_button_mask;
  GtkTargetList     *target_list;        /* Targets for drag data */
  GdkDragAction      actions;            /* Possible actions */
  GdkColormap       *colormap;	         /* Colormap for drag icon */
  GdkPixmap         *pixmap;             /* Icon for drag data */
  GdkBitmap         *mask;

  /* Stored button press information to detect drag beginning */
  gint               state;
  gint               x, y;
};
  
struct _GtkDragSourceInfo {
  GtkWidget         *widget;
  GtkTargetList     *target_list; /* Targets for drag data */
61
  GdkDragAction      possible_actions; /* Actions allowed by source */
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  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 */

  GtkDragStatus      status;	  /* drag status */
  GdkEvent          *last_event;  /* motion event waiting for response */

  gint               start_x, start_y; /* Initial position */
  gint               cur_x, cur_y;     /* Current Position */

  GList             *selections;  /* selections we've claimed */
  
  GtkDragDestInfo   *proxy_dest;  /* Set if this is a proxy drag */

  guint              drop_timeout; /* Timeout for aborting drop */
};

struct _GtkDragDestSite {
  GtkDestDefaults    flags;
  GtkTargetList     *target_list;
  GdkDragAction      actions;
  GdkWindow         *proxy_window;
  GdkDragProtocol    proxy_protocol;
88
  gboolean           do_proxy : 1;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  gboolean           proxy_coords : 1;
  gboolean           have_drag : 1;
};
  
struct _GtkDragDestInfo {
  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 */
};

107
#define DROP_ABORT_TIME 300000
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

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

struct _GtkDragAnim {
  GtkDragSourceInfo *info;
  gint step;
  gint n_steps;
};

struct _GtkDragFindData {
  gint x;
  gint y;
  GdkDragContext *context;
  GtkDragDestInfo *info;
  gboolean found;
126
  gboolean toplevel;
127 128 129 130 131 132 133 134
  gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
			gint x, gint y, guint32 time);
  guint32 time;
};

/* Enumeration for some targets we handle internally */

enum {
135
  TARGET_MOTIF_SUCCESS = 0x40000000,
136 137 138 139 140 141 142 143 144 145 146 147 148
  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 */
149 150 151 152 153
static void gtk_drag_get_event_actions   (GdkEvent        *event, 
					  gint             button,
					  GdkDragAction    actions,
					  GdkDragAction   *suggested_action,
					  GdkDragAction   *possible_actions);
154 155 156 157
static GdkCursor *   gtk_drag_get_cursor         (GdkDragAction action);
static GtkWidget    *gtk_drag_get_ipc_widget     (void);
static void          gtk_drag_release_ipc_widget (GtkWidget *widget);

158 159
static GdkAtom   gtk_drag_dest_find_target    (GtkWidget          *widget,
					       GtkDragDestSite    *site,
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
			                       GdkDragContext     *context);
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);
static void      gtk_drag_dest_info_destroy   (gpointer            data);
static void      gtk_drag_dest_realized       (GtkWidget          *widget);
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);

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);
static void gtk_drag_source_info_destroy       (gpointer           data);
static gint gtk_drag_motion_cb                 (GtkWidget         *widget, 
					        GdkEventMotion    *event, 
					        gpointer           data);
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
Owen Taylor's avatar
Owen Taylor committed
223
static const char action_ask_bits[] = {
224 225 226 227 228 229
  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
Owen Taylor's avatar
Owen Taylor committed
230
static const char action_ask_mask_bits[] = {
231 232 233 234 235 236
  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
Owen Taylor's avatar
Owen Taylor committed
237
static const char action_copy_bits[] = {
238 239 240 241 242 243
  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
Owen Taylor's avatar
Owen Taylor committed
244
static const char action_copy_mask_bits[] = {
245 246 247 248 249 250
  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
Owen Taylor's avatar
Owen Taylor committed
251
static const char action_move_bits[] = {
252 253 254 255 256 257
  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
Owen Taylor's avatar
Owen Taylor committed
258
static const char action_move_mask_bits[] = {
259 260 261 262 263 264
  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
Owen Taylor's avatar
Owen Taylor committed
265
static const char action_link_bits[] = {
266 267 268 269 270 271
  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
Owen Taylor's avatar
Owen Taylor committed
272
static const char action_link_mask_bits[] = {
273 274 275 276 277 278
  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
Owen Taylor's avatar
Owen Taylor committed
279
static const char action_none_bits[] = {
280 281 282 283 284 285
  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
Owen Taylor's avatar
Owen Taylor committed
286
static const char action_none_mask_bits[] = {
287 288 289 290 291 292 293 294 295
  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

static struct {
  GdkDragAction action;
Owen Taylor's avatar
Owen Taylor committed
296 297
  const char *bits;
  const char *mask;
298 299 300 301 302 303 304 305 306 307
  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 },
};

308
static const gint n_drag_cursors = sizeof(drag_cursors) / sizeof(drag_cursors[0]);
309 310

/* XPM */
311
static const char * drag_default_xpm[] = {
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
"32 32 3 1",
" 	c None",
".	c #000000",
"+	c #FFFFFF",
"                                ",
"                                ",
"                ..              ",
"              ..+.              ",
"             ..++..             ",
"           ...++++.             ",
"         ...++++++..            ",
"       ...+++++++++.            ",
"     ...+++++++++++..           ",
"    ..+.++++++++++++..          ",
"     .++.++++++++++++..         ",
"     .+++.++++++++++++..        ",
"     .++++.++++++++++++.        ",
"     .+++.+++++++++++++..       ",
"     .++.+++++++++++++++..      ",
"     .+.+++++++++++++++++..     ",
"     ..+++++++++++++++++++..    ",
"     ..++++++++++++++++++++.    ",
"     .++++++++++++++++++++..    ",
"     ..+++++++++++++++++..      ",
"      .++++++++++++++++..       ",
"      ..+++++++++++++...        ",
"       .++++++++++++..          ",
"       ..+++++++++..            ",
"        .++++++++..             ",
"        ..++++++..              ",
"         .+++++..               ",
"          .++..                 ",
"           ...                  ",
"           ..                   ",
"                                ",
"                                "};

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

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

static GtkWidget *
gtk_drag_get_ipc_widget(void)
{
  GtkWidget *result;
  
  if (drag_widgets)
    {
      GSList *tmp = drag_widgets;
      result = drag_widgets->data;
      drag_widgets = drag_widgets->next;
      g_slist_free_1 (tmp);
    }
  else
    {
      result = gtk_invisible_new();
      gtk_widget_show (result);
    }

  return result;
}

/*************************************************************
 * gtk_drag_release_ipc_widget:
 *     Releases widget retrieved with gtk_drag_get_ipc_widget()
 *   arguments:
 *     widget: the widget to release.
 *   results:
 *************************************************************/

static void
gtk_drag_release_ipc_widget (GtkWidget *widget)
{
  drag_widgets = g_slist_prepend (drag_widgets, widget);
}

397 398 399 400 401 402
static void
gtk_drag_get_event_actions (GdkEvent *event, 
			    gint button, 
			    GdkDragAction  actions,
			    GdkDragAction *suggested_action,
			    GdkDragAction *possible_actions)
403
{
404 405 406
  *suggested_action = 0;
  *possible_actions = 0;

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
  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;
      case GDK_ENTER_NOTIFY:
      case GDK_LEAVE_NOTIFY:
	  state = event->crossing.state;
	  break;
      default:
	break;
      }
      
434
      if (((button == 2) || (button == 3)) && (actions & GDK_ACTION_ASK))
435 436 437 438 439
	{
	  *suggested_action = GDK_ACTION_ASK;
	  *possible_actions = actions;
	}
      else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
440 441
	{
	  if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
442 443 444 445 446 447 448
	    {
	      if (actions & GDK_ACTION_LINK)
		{
		  *suggested_action = GDK_ACTION_LINK;
		  *possible_actions = GDK_ACTION_LINK;
		}
	    }
449
	  else if (state & GDK_CONTROL_MASK)
450 451 452 453 454 455 456 457
	    {
	      if (actions & GDK_ACTION_COPY)
		{
		  *suggested_action = GDK_ACTION_COPY;
		  *possible_actions = GDK_ACTION_COPY;
		}
	      return;
	    }
458
	  else
459 460 461 462 463 464 465 466
	    {
	      if (actions & GDK_ACTION_MOVE)
		{
		  *suggested_action = GDK_ACTION_MOVE;
		  *possible_actions = GDK_ACTION_MOVE;
		}
	      return;
	    }
467 468 469
	}
      else
	{
470
	  *possible_actions = actions;
471

472 473 474 475
	  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;
476
	  else if (actions & GDK_ACTION_MOVE)
477
	    *suggested_action = GDK_ACTION_MOVE;
478
	  else if (actions & GDK_ACTION_LINK)
479
	    *suggested_action = GDK_ACTION_LINK;
480 481 482
	}
    }
  
483
  return;
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 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 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
}

static GdkCursor *
gtk_drag_get_cursor (GdkDragAction action)
{
  gint i;
  
  for (i = 0 ; i < n_drag_cursors - 1; i++)
    if (drag_cursors[i].action == action)
      break;

  if (drag_cursors[i].cursor == NULL)
    {
      GdkColor fg, bg;

      GdkPixmap *pixmap = 
	gdk_bitmap_create_from_data (NULL, 
				     drag_cursors[i].bits,
				     CURSOR_WIDTH, CURSOR_HEIGHT);
      GdkPixmap *mask = 
	gdk_bitmap_create_from_data (NULL, 
				     drag_cursors[i].mask,
				     CURSOR_WIDTH, CURSOR_HEIGHT);

      gdk_color_white (gdk_colormap_get_system(), &bg);
      gdk_color_black (gdk_colormap_get_system(), &fg);
      
      drag_cursors[i].cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, 0, 0);

      gdk_pixmap_unref (pixmap);
      gdk_pixmap_unref (mask);
    }

  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;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (context != NULL);

  selection_widget = gtk_drag_get_ipc_widget();

  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);

  gtk_object_set_data (GTK_OBJECT (selection_widget), "drag-context", context);

  gtk_selection_convert (selection_widget,
			 gdk_drag_get_selection(context),
			 target,
			 time);
}

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 596 597

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

GtkWidget *
gtk_drag_get_source_widget (GdkDragContext *context)
{
  GSList *tmp_list;

  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");

	  return info ? info->widget : NULL;
	}

      tmp_list = tmp_list->next;
    }

  return NULL;
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611
/*************************************************************
 * 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,
612
		 gboolean        del,
613 614 615 616 617 618
		 guint32         time)
{
  GdkAtom target = GDK_NONE;

  g_return_if_fail (context != NULL);

619
  if (success && del)
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
    {
      target = gdk_atom_intern ("DELETE", FALSE);
    }
  else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
    {
      target = gdk_atom_intern (success ? 
				  "XmTRANSFER_SUCCESS" : 
				  "XmTRANSFER_FAILURE",
				FALSE);
    }

  if (target != GDK_NONE)
    {
      GtkWidget *selection_widget = gtk_drag_get_ipc_widget();

      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,
			     gdk_drag_get_selection(context),
			     target,
			     time);
    }
  
648
  if (!del)
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    gdk_drop_finish (context, success, time);
}

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

void 
gtk_drag_highlight (GtkWidget  *widget)
{
  gint x, y;

  g_return_if_fail (widget != NULL);

  if (GTK_WIDGET_NO_WINDOW (widget))
    {
      x = widget->allocation.x;
      y = widget->allocation.y;
    }
  else
    {
      x = 0;
      y = 0;
    }
	
  gtk_draw_shadow (widget->style, widget->window,
		   GTK_STATE_NORMAL, GTK_SHADOW_OUT,
		   x, y, 
		   widget->allocation.width,
		   widget->allocation.height);

  gdk_draw_rectangle (widget->window,
		      widget->style->black_gc,
		      FALSE,
		      x, y,
		      widget->allocation.width - 1,
		      widget->allocation.height - 1);
}

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

void 
gtk_drag_unhighlight (GtkWidget  *widget)
{
  gint x, y;

  g_return_if_fail (widget != NULL);

  if (GTK_WIDGET_NO_WINDOW (widget))
    {
      x = widget->allocation.x;
      y = widget->allocation.y;
    }
  else
    {
      x = 0;
      y = 0;
    }
	
  gdk_window_clear_area_e (widget->window,
			   x, y,
			   widget->allocation.width,
			   widget->allocation.height);
}

/*************************************************************
 * 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 
737 738 739 740 741
gtk_drag_dest_set   (GtkWidget            *widget,
		     GtkDestDefaults       flags,
		     const GtkTargetEntry *targets,
		     gint                  n_targets,
		     GdkDragAction         actions)
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
{
  GtkDragDestSite *site;
  
  g_return_if_fail (widget != NULL);

  /* HACK, do this in the destroy */
  site = gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest");
  if (site)
    gtk_signal_disconnect_by_data (GTK_OBJECT (widget), 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), NULL);

  site = g_new (GtkDragDestSite, 1);

  site->flags = flags;
  site->have_drag = FALSE;
  if (targets)
    site->target_list = gtk_target_list_new (targets, n_targets);
  else
    site->target_list = NULL;

  site->actions = actions;
768
  site->do_proxy = FALSE;
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

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

/*************************************************************
 * 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);

  /* HACK, do this in the destroy */
  site = gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest");
  if (site)
    gtk_signal_disconnect_by_data (GTK_OBJECT (widget), 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), NULL);

  site = g_new (GtkDragDestSite, 1);

  site->flags = 0;
  site->have_drag = FALSE;
  site->target_list = NULL;
  site->actions = 0;
  site->proxy_window = proxy_window;
815 816 817
  if (proxy_window)
    gdk_window_ref (proxy_window);
  site->do_proxy = TRUE;
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 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 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
  site->proxy_protocol = protocol;
  site->proxy_coords = use_coordinates;

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

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

void 
gtk_drag_dest_unset (GtkWidget        *widget)
{
  g_return_if_fail (widget != NULL);

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

/*************************************************************
 * gtk_drag_dest_handle_event:
 *     Called from widget event handling code on Drag events
 *     for destinations.
 *
 *   arguments:
 *     toplevel: Toplevel widget that received the event
 *     event:
 *   results:
 *************************************************************/

void
gtk_drag_dest_handle_event (GtkWidget *toplevel,
			    GdkEvent  *event)
{
  GtkDragDestInfo *info;
  GdkDragContext *context;

  g_return_if_fail (toplevel != NULL);
  g_return_if_fail (event != NULL);

  context = event->dnd.context;

  info = g_dataset_get_data (context, "gtk-info");
  if (!info)
    {
      info = g_new (GtkDragDestInfo, 1);
      info->widget = NULL;
      info->context = event->dnd.context;
      info->proxy_source = NULL;
      info->proxy_data = NULL;
      info->dropped = FALSE;
      info->proxy_drop_wait = FALSE;
      g_dataset_set_data_full (context,
			       "gtk-info",
			       info,
			       gtk_drag_dest_info_destroy);
    }

  /* 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:
      {
	GtkDragFindData data;
	gint tx, ty;

	if (event->type == GDK_DROP_START)
	  info->dropped = TRUE;

903
	gdk_window_get_origin (toplevel->window, &tx, &ty);
904 905 906

	data.x = event->dnd.x_root - tx;
	data.y = event->dnd.y_root - ty;
907
 	data.context = context;
908 909
	data.info = info;
	data.found = FALSE;
910
	data.toplevel = TRUE;
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
	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);

	/* We send a leave here so that the widget unhighlights
	 * properly
	 */
	if (info->widget &&
	    ((event->type == GDK_DROP_START) || (!data.found)))
	  {
	    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);
	  }
	else if (event->type == GDK_DROP_START)
935 936
	  {
	    gdk_drop_reply (context, data.found, event->dnd.time);
937
            if ((context->protocol == GDK_DRAG_PROTO_MOTIF) && !data.found)
938 939
	      gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
	  }
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
      }
      break;

    default:
      g_assert_not_reached();
    }
}

/*************************************************************
 * gtk_drag_dest_find_target:
 *     Decide on a target for the drag.
 *   arguments:
 *     site:
 *     context:
 *   results:
 *************************************************************/

static GdkAtom
958 959
gtk_drag_dest_find_target (GtkWidget       *widget,
			   GtkDragDestSite *site,
960 961 962 963
			   GdkDragContext  *context)
{
  GList *tmp_target;
  GList *tmp_source = NULL;
964
  GtkWidget *source_widget = gtk_drag_get_source_widget (context);
965 966 967 968 969 970 971 972 973

  tmp_target = site->target_list->list;
  while (tmp_target)
    {
      GtkTargetPair *pair = tmp_target->data;
      tmp_source = context->targets;
      while (tmp_source)
	{
	  if (tmp_source->data == GUINT_TO_POINTER (pair->target))
974 975 976 977 978 979 980
	    {
	      if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
		  (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
		return pair->target;
	      else
		break;
	    }
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	  tmp_source = tmp_source->next;
	}
      tmp_target = tmp_target->next;
    }

  return GDK_NONE;
}

static void
gtk_drag_selection_received (GtkWidget          *widget,
			     GtkSelectionData   *selection_data,
			     guint32             time,
			     gpointer            data)
{
  GdkDragContext *context;
  GtkDragDestInfo *info;
  GtkWidget *drop_widget;

  drop_widget = data;

  context = gtk_object_get_data (GTK_OBJECT (widget), "drag-context");
  info = g_dataset_get_data (context, "gtk-info");

  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);
      gtk_main_quit();
      return;
    }

  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)) ||
1021
	   (selection_data->target == gdk_atom_intern ("XmTRANSFER_FAILURE", FALSE)))
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
    {
      /* Do nothing */
    }
  else
    {
      GtkDragDestSite *site;

      site = gtk_object_get_data (GTK_OBJECT (drop_widget), "gtk-drag-dest");

      if (site->target_list)
	{
	  guint target_info;

	  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);
	}
      
      if (site->flags & GTK_DEST_DEFAULT_DROP)
	{

	  gtk_drag_finish (context, 
			   (selection_data->length >= 0),
			   (context->action == GDK_ACTION_MOVE),
			   time);
	}
      
      gtk_widget_unref (drop_widget);
    }

  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);

  gtk_drag_release_ipc_widget (widget);
}

/*************************************************************
 * 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;
  gint x_offset = 0;
  gint y_offset = 0;

  new_allocation = widget->allocation;

1097
  if (data->found || !GTK_WIDGET_MAPPED (widget))
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
    return;

  if (!GTK_WIDGET_NO_WINDOW (widget))
    {
      new_allocation.x = 0;
      new_allocation.y = 0;
    }
  
  if (widget->parent)
    {
      GdkWindow *window = widget->window;
      while (window != widget->parent->window)
	{
	  gint tx, ty, twidth, theight;
	  gdk_window_get_size (window, &twidth, &theight);

	  if (new_allocation.x < 0)
	    {
	      new_allocation.width += new_allocation.x;
	      new_allocation.x = 0;
	    }
	  if (new_allocation.y < 0)
	    {
	      new_allocation.height += new_allocation.y;
	      new_allocation.y = 0;
	    }
	  if (new_allocation.x + new_allocation.width > twidth)
	    new_allocation.width = twidth - new_allocation.x;
	  if (new_allocation.y + new_allocation.height > theight)
	    new_allocation.height = theight - new_allocation.y;

	  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);
	}
    }

1139 1140 1141 1142
  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)))
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    {
      /* 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;
	  
	  new_data.x -= x_offset;
	  new_data.y -= y_offset;
	  new_data.found = FALSE;
1154
	  new_data.toplevel = FALSE;
1155
	  
1156 1157 1158
	  gtk_container_forall (GTK_CONTAINER (widget),
				(GtkCallback)gtk_drag_find_widget,
				&new_data);
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	  
	  data->found = new_data.found;
	}

      /* 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,
					data->x - new_allocation.x,
					data->y - new_allocation.y,
					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
gtk_drag_proxy_begin (GtkWidget *widget, GtkDragDestInfo *dest_info)
{
  GtkDragSourceInfo *source_info;
  GList *tmp_list;
  
  source_info = g_new0 (GtkDragSourceInfo, 1);
  source_info->ipc_widget = gtk_drag_get_ipc_widget ();
  
  source_info->widget = widget;
  gtk_widget_ref (source_info->widget);
  source_info->context = gdk_drag_begin (source_info->ipc_widget->window,
1200
					 dest_info->context->targets);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

  source_info->target_list = gtk_target_list_new (NULL, 0);
  tmp_list = dest_info->context->targets;
  while (tmp_list)
    {
      gtk_target_list_add (source_info->target_list, 
			   GPOINTER_TO_UINT (tmp_list->data), 0, 0);
      tmp_list = tmp_list->next;
    }

  source_info->proxy_dest = dest_info;
  
  g_dataset_set_data (source_info->context, "gtk-info", source_info);
  
  gtk_signal_connect (GTK_OBJECT (source_info->ipc_widget), 
		      "selection_get",
		      GTK_SIGNAL_FUNC (gtk_drag_selection_get), 
		      source_info);
  
  dest_info->proxy_source = source_info;
}

static void
gtk_drag_dest_info_destroy (gpointer data)
{
  GtkDragDestInfo *info = data;

  g_free (info);
}

static void
gtk_drag_dest_realized (GtkWidget *widget)
{
  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
  gdk_window_register_dnd (toplevel->window);
}

static void
gtk_drag_dest_site_destroy (gpointer data)
{
  GtkDragDestSite *site = data;

  if (site->target_list)
    gtk_target_list_unref (site->target_list);

  g_free (site);
}

/*
 * Default drag handlers
 */
static void  
gtk_drag_dest_leave (GtkWidget      *widget,
		     GdkDragContext *context,
		     guint           time)
{
  GtkDragDestSite *site;

  site = gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest");
  g_return_if_fail (site != NULL);

1262
  if (site->do_proxy)
1263 1264 1265 1266 1267 1268 1269 1270
    {
      GtkDragDestInfo *info = g_dataset_get_data (context, "gtk-info");

      if (info->proxy_source && !info->dropped)
	gdk_drag_abort (info->proxy_source->context, time);
      
      return;
    }
1271
  else
1272
    {
1273 1274 1275
      if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
	gtk_drag_unhighlight (widget);

1276 1277 1278 1279
      if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag)
	gtk_signal_emit_by_name (GTK_OBJECT (widget), "drag_leave",
				 context, time);
      
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
      site->have_drag = FALSE;
    }
}

static gboolean
gtk_drag_dest_motion (GtkWidget	     *widget,
		      GdkDragContext *context,
		      gint            x,
		      gint            y,
		      guint           time)
{
  GtkDragDestSite *site;
  GdkDragAction action = 0;
  gboolean retval;

  site = gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest");
  g_return_val_if_fail (site != NULL, FALSE);

1298
  if (site->do_proxy)
1299 1300 1301
    {
      GdkAtom selection;
      GdkEvent *current_event;
1302 1303 1304
      GdkWindow *dest_window;
      GdkDragProtocol proto;
	
1305 1306 1307 1308 1309 1310 1311
      GtkDragDestInfo *info = g_dataset_get_data (context, "gtk-info");

      if (!info->proxy_source)
	gtk_drag_proxy_begin (widget, info);

      current_event = gtk_get_current_event ();

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
      if (site->proxy_window)
	{
	  dest_window = site->proxy_window;
	  proto = site->proxy_protocol;
	}
      else
	{
	  gdk_drag_find_window (info->proxy_source->context,
				NULL,
				current_event->dnd.x_root, 
				current_event->dnd.y_root,
				&dest_window, &proto);
	}
      
1326
      gdk_drag_motion (info->proxy_source->context, 
1327
		       dest_window, proto,
1328 1329
		       current_event->dnd.x_root, 
		       current_event->dnd.y_root, 
1330 1331
		       context->suggested_action, 
		       context->actions, time);
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

      selection = gdk_drag_get_selection (info->proxy_source->context);
      if (selection && 
	  selection != gdk_drag_get_selection (info->context))
	gtk_drag_source_check_selection (info->proxy_source, selection, time);

      gdk_event_free (current_event);
      
      return TRUE;
    }

  if (site->flags & GTK_DEST_DEFAULT_MOTION)
    {
      if (context->suggested_action & site->actions)
	action = context->suggested_action;
      else
	{
	  gint i;
	  
	  for (i=0; i<8; i++)
	    {
	      if ((site->actions & (1 << i)) &&
		  (context->actions & (1 << i)))
		{
		  action = (1 << i);
		  break;
		}
	    }
	}
      
1362
      if (action && gtk_drag_dest_find_target (widget, site, context))
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
	{
	  if (!site->have_drag)
	    {
	      site->have_drag = TRUE;
	      if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
		gtk_drag_highlight (widget);
	    }
	  
	  gdk_drag_status (context, action, time);
	}
      else
	{
	  gdk_drag_status (context, 0, time);
	  return TRUE;
	}
    }

  gtk_signal_emit_by_name (GTK_OBJECT (widget), "drag_motion",
			   context, x, y, time, &retval);

  return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
}

static gboolean
gtk_drag_dest_drop (GtkWidget	     *widget,
		    GdkDragContext   *context,
		    gint              x,
		    gint              y,
		    guint             time)
{
  GtkDragDestSite *site;
  GtkDragDestInfo *info;

  site = gtk_object_get_data (GTK_OBJECT (widget), "gtk-drag-dest");
  g_return_val_if_fail (site != NULL, FALSE);

  info = g_dataset_get_data (context, "gtk-info");
  g_return_val_if_fail (info != NULL, FALSE);

  info->drop_x = x;
  info->drop_y = y;

1405
  if (site->do_proxy)
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
    {
      if (info->proxy_source || 
	  (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN))
	{
	  gtk_drag_drop (info->proxy_source, time);
	}
      else
	{
	  /* We need to synthesize a motion event, wait for a status,
	   * and, if we get one a good one, do a drop.
	   */
	  
	  GdkEvent *current_event;
	  GdkAtom selection;
1420 1421
	  GdkWindow *dest_window;
	  GdkDragProtocol proto;
1422 1423 1424 1425 1426 1427 1428
	  
	  gtk_drag_proxy_begin (widget, info);
	  info->proxy_drop_wait = TRUE;
	  info->proxy_drop_time = time;
	  
	  current_event = gtk_get_current_event ();

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
	  if (site->proxy_window)
	    {
	      dest_window = site->proxy_window;
	      proto = site->proxy_protocol;
	    }
	  else
	    {
	      gdk_drag_find_window (info->proxy_source->context,
				    NULL,
				    current_event->dnd.x_root, 
				    current_event->dnd.y_root,
				    &dest_window, &proto);
	    }
	    
1443
	  gdk_drag_motion (info->proxy_source->context, 
1444
			   dest_window, proto,
1445 1446
			   current_event->dnd.x_root, 
			   current_event->dnd.y_root, 
1447 1448
			   context->suggested_action, 
			   context->actions, time);
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464

	  selection = gdk_drag_get_selection (info->proxy_source->context);
	  if (selection && 
	      selection != gdk_drag_get_selection (info->context))
	    gtk_drag_source_check_selection (info->proxy_source, selection, time);

	  gdk_event_free (current_event);
      
	}

      return TRUE;
    }
  else
    {
      gboolean retval;

1465
      if (site->flags & GTK_DEST_DEFAULT_DROP)
1466
	{
1467
	  GdkAtom target = gtk_drag_dest_find_target (widget, site, context);
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
      
	  if (target == GDK_NONE)
	    return FALSE;
	  
	  gtk_drag_get_data (widget, context, target, time);
	}

      gtk_signal_emit_by_name (GTK_OBJECT (widget), "drag_drop",
			       context, x, y, time, &retval);

1478
      return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
    }
}

/***************
 * Source side *
 ***************/

/*************************************************************
 * gtk_drag_begin: Start a drag operation
 *     
 *   arguments:
 *     widget:   Widget from which drag starts
 *     handlers: List of handlers to supply the data for the drag
 *     button:   Button user used to start drag
 *     time:     Time of event starting drag
 *
 *   results:
 *************************************************************/

GdkDragContext *
gtk_drag_begin (GtkWidget         *widget,
		GtkTargetList     *target_list,
		GdkDragAction      actions,
		gint               button,
		GdkEvent          *event)
{
  GtkDragSourceInfo *info;
  GList *targets = NULL;
  GList *tmp_list;
  guint32 time = GDK_CURRENT_TIME;
1509
  GdkDragAction possible_actions, suggested_action;
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

  g_return_val_if_fail (widget != NULL, NULL);
  g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
  g_return_val_if_fail (target_list != NULL, NULL);

  if (event)
    time = gdk_event_get_time (event);

  info = g_new0 (GtkDragSourceInfo, 1);
  info->ipc_widget = gtk_drag_get_ipc_widget ();
1520 1521 1522
  source_widgets = g_slist_prepend (source_widgets, info->ipc_widget);

  gtk_object_set_data (GTK_OBJECT (info->ipc_widget), "gtk-info", info);
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535

  tmp_list = g_list_last (target_list->list);
  while (tmp_list)
    {
      GtkTargetPair *pair = tmp_list->data;
      targets = g_list_prepend (targets, 
				GINT_TO_POINTER (pair->target));
      tmp_list = tmp_list->prev;
    }

  info->widget = widget;
  gtk_widget_ref (info->widget);
  
1536
  info->context = gdk_drag_begin (info->ipc_widget->window, targets);
1537 1538 1539 1540 1541 1542 1543 1544
  g_list_free (targets);
  
  g_dataset_set_data (info->context, "gtk-info", info);

  info->button = button;
  info->target_list = target_list;
  gtk_target_list_ref (target_list);

1545 1546
  info->possible_actions = actions;

1547 1548 1549 1550 1551
  info->cursor = NULL;
  info->status = GTK_DRAG_STATUS_DRAG;
  info->last_event = NULL;
  info->selections = NULL;
  info->icon_window = NULL;
1552 1553 1554

  gtk_drag_get_event_actions (event, info->button, actions,
			      &suggested_action, &possible_actions);
1555 1556
  
  if (event)
1557
    info->cursor = gtk_drag_get_cursor (suggested_action);
1558

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
  /* Set cur_x, cur_y here so if the "drag_begin" signal shows
   * the drag icon, it will be in the right place
   */
  if (event->type == GDK_MOTION_NOTIFY)
    {
      info->cur_x = event->motion.x_root;
      info->cur_y = event->motion.y_root;
    }
  else 
    {
      gint x, y;
      gdk_window_get_pointer (GDK_ROOT_PARENT(), &x, &y, NULL);

      info->cur_x = x;
      info->cur_y = y;
    }

1576
  gtk_signal_emit_by_name (GTK_OBJECT (widget), "drag_begin",
1577
			   info->context);
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
  
  /* We use a GTK grab here to override any grabs that the widget
   * we are dragging from might have held
   */

  gtk_grab_add (info->ipc_widget);
  gdk_pointer_grab (info->ipc_widget->window, FALSE,
		    GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
		    GDK_BUTTON_RELEASE_MASK, NULL,
		    info->cursor, time);

  if (event->type == GDK_MOTION_NOTIFY)
    gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);

  info->start_x = info->cur_x;
  info->start_y = info->cur_y;

  gtk_signal_connect (GTK_OBJECT (info->ipc_widget), "button_release_event",
		      GTK_SIGNAL_FUNC (gtk_drag_button_release_cb), info);
  gtk_signal_connect (GTK_OBJECT (info->ipc_widget), "motion_notify_event",
		      GTK_SIGNAL_FUNC (gtk_drag_motion_cb), info);
  gtk_signal_connect (GTK_OBJECT (info->ipc_widget), "selection_get",
		      GTK_SIGNAL_FUNC (gtk_drag_selection_get), info);

  return info->context;
}

/*************************************************************
 * gtk_drag_source_set:
 *     Register a drop site, and possibly add default behaviors.
 *   arguments:
 *     widget:
 *     start_button_mask: Mask of allowed buttons to start drag
 *     targets:           Table of targets for this source
 *     n_targets:
 *     actions:           Actions allowed for this source
 *   results:
 *************************************************************/

void 
1618 1619 1620 1621 1622
gtk_drag_source_set  (GtkWidget            *widget,
		      GdkModifierType       start_button_mask,
		      const GtkTargetEntry *targets,
		      gint                  n_targets,
		      GdkDragAction         actions)
1623 1624 1625 1626 1627 1628 1629