gtkdnd.c 115 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
#include <config.h>
28 29 30 31

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

32 33
#include "gdkconfig.h"

34
#include "gdk/gdkkeysyms.h"
35 36

#include "gtkdnd.h"
37 38
#include "gtkiconfactory.h"
#include "gtkicontheme.h"
39
#include "gtkimage.h"
40 41
#include "gtkinvisible.h"
#include "gtkmain.h"
42
#include "gtkplug.h"
43
#include "gtkstock.h"
Owen Taylor's avatar
Owen Taylor committed
44
#include "gtkwindow.h"
Matthias Clasen's avatar
Matthias Clasen committed
45
#include "gtkintl.h"
46
#include "gtkdndcursors.h"
47
#include "gtkalias.h"
48

49
static GSList *source_widgets = NULL;
50 51 52 53 54 55 56 57 58

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
59 60
typedef enum 
{
61 62 63 64 65
  GTK_DRAG_STATUS_DRAG,
  GTK_DRAG_STATUS_WAIT,
  GTK_DRAG_STATUS_DROP
} GtkDragStatus;

Owen Taylor's avatar
Owen Taylor committed
66 67
struct _GtkDragSourceSite 
{
68 69 70
  GdkModifierType    start_button_mask;
  GtkTargetList     *target_list;        /* Targets for drag data */
  GdkDragAction      actions;            /* Possible actions */
71 72 73 74 75 76 77 78

  /* Drag icon */
  GtkImageType icon_type;
  union
  {
    GtkImagePixmapData pixmap;
    GtkImagePixbufData pixbuf;
    GtkImageStockData stock;
79
    GtkImageIconNameData name;
80
  } icon_data;
Havoc Pennington's avatar
Havoc Pennington committed
81
  GdkBitmap *icon_mask;
82

83
  GdkColormap       *colormap;	         /* Colormap for drag icon */
Owen Taylor's avatar
Owen Taylor committed
84

85 86 87 88
  /* Stored button press information to detect drag beginning */
  gint               state;
  gint               x, y;
};
Owen Taylor's avatar
Owen Taylor committed
89
  
Owen Taylor's avatar
Owen Taylor committed
90 91
struct _GtkDragSourceInfo 
{
92 93
  GtkWidget         *widget;
  GtkTargetList     *target_list; /* Targets for drag data */
94
  GdkDragAction      possible_actions; /* Actions allowed by source */
95 96
  GdkDragContext    *context;	  /* drag context */
  GtkWidget         *icon_window; /* Window for drag */
97
  GtkWidget         *fallback_icon; /* Window for drag used on other screens */
98 99 100 101
  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
102

103
  GtkDragStatus      status;	  /* drag status */
104
  GdkEvent          *last_event;  /* pending event */
Owen Taylor's avatar
Owen Taylor committed
105

106 107
  gint               start_x, start_y; /* Initial position */
  gint               cur_x, cur_y;     /* Current Position */
108
  GdkScreen         *cur_screen;       /* Current screen for pointer */
Owen Taylor's avatar
Owen Taylor committed
109

110
  guint32            grab_time;   /* timestamp for initial grab */
111 112 113
  GList             *selections;  /* selections we've claimed */
  
  GtkDragDestInfo   *proxy_dest;  /* Set if this is a proxy drag */
Owen Taylor's avatar
Owen Taylor committed
114

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

Owen Taylor's avatar
Owen Taylor committed
125 126
struct _GtkDragDestSite 
{
127 128 129 130 131
  GtkDestDefaults    flags;
  GtkTargetList     *target_list;
  GdkDragAction      actions;
  GdkWindow         *proxy_window;
  GdkDragProtocol    proxy_protocol;
132 133 134
  guint              do_proxy : 1;
  guint              proxy_coords : 1;
  guint              have_drag : 1;
135
  guint              track_motion : 1;
136
};
Owen Taylor's avatar
Owen Taylor committed
137
  
Owen Taylor's avatar
Owen Taylor committed
138 139
struct _GtkDragDestInfo 
{
140 141 142 143
  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 */
144
  guint              dropped : 1;     /* Set after we receive a drop */
145
  guint32            proxy_drop_time; /* Timestamp for proxied drop */
146
  guint              proxy_drop_wait : 1; /* Set if we are waiting for a
147 148 149 150 151 152
					   * status reply before sending
					   * a proxied drop on.
					   */
  gint               drop_x, drop_y; /* Position of drop */
};

153
#define DROP_ABORT_TIME 300000
154 155 156 157 158 159

#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
160 161
struct _GtkDragAnim 
{
162 163 164 165 166
  GtkDragSourceInfo *info;
  gint step;
  gint n_steps;
};

Owen Taylor's avatar
Owen Taylor committed
167 168
struct _GtkDragFindData 
{
169 170 171 172 173
  gint x;
  gint y;
  GdkDragContext *context;
  GtkDragDestInfo *info;
  gboolean found;
174
  gboolean toplevel;
175 176 177 178 179 180 181
  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
182
enum {
183
  TARGET_MOTIF_SUCCESS = 0x40000000,
184 185 186 187 188 189 190 191 192 193 194 195 196
  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
197 198 199 200 201
static void          gtk_drag_get_event_actions (GdkEvent        *event, 
					         gint             button,
					         GdkDragAction    actions,
					         GdkDragAction   *suggested_action,
					         GdkDragAction   *possible_actions);
202
static GdkCursor *   gtk_drag_get_cursor         (GdkDisplay     *display,
203 204
						  GdkDragAction   action,
						  GtkDragSourceInfo *info);
205
static void          gtk_drag_update_cursor      (GtkDragSourceInfo *info);
Matthias Clasen's avatar
Matthias Clasen committed
206 207
static GtkWidget    *gtk_drag_get_ipc_widget            (GtkWidget *widget);
static GtkWidget    *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
Owen Taylor's avatar
Owen Taylor committed
208
static void          gtk_drag_release_ipc_widget (GtkWidget      *widget);
209

Owen Taylor's avatar
Owen Taylor committed
210 211 212
static gboolean      gtk_drag_highlight_expose   (GtkWidget      *widget,
					  	  GdkEventExpose *event,
						  gpointer        data);
213

214 215
static void     gtk_drag_selection_received     (GtkWidget        *widget,
						 GtkSelectionData *selection_data,
216
						 guint             time,
217 218 219 220 221 222 223 224
						 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
225
						 GtkWidget        *previous_toplevel);
226 227 228 229 230 231 232 233 234 235 236 237 238 239
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
240 241 242 243 244 245

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);
246 247 248 249 250 251 252 253 254

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,
255
						GtkDragResult      result,
256
						guint              time);
257
static void gtk_drag_cancel                    (GtkDragSourceInfo *info,
258
						GtkDragResult      result,
259
						guint32            time);
260

261
static gboolean gtk_drag_source_event_cb       (GtkWidget         *widget,
262 263 264 265 266 267 268 269
						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);
270
static gboolean gtk_drag_anim_timeout          (gpointer           data);
271
static void gtk_drag_remove_icon               (GtkDragSourceInfo *info);
Owen Taylor's avatar
Owen Taylor committed
272
static void gtk_drag_source_info_destroy       (GtkDragSourceInfo *info);
273 274
static void gtk_drag_add_update_idle           (GtkDragSourceInfo *info);

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

297 298 299 300 301 302 303
static void     set_icon_stock_pixbuf          (GdkDragContext    *context,
						const gchar       *stock_id,
						GdkPixbuf         *pixbuf,
						gint               hot_x,
						gint               hot_y,
						gboolean           force_window);

304 305 306 307
/************************
 * Cursor and Icon data *
 ************************/

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

Owen Taylor's avatar
Owen Taylor committed
323
static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
324 325 326 327 328

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

Manish Singh's avatar
Manish Singh committed
329
static void
330 331 332 333 334
set_can_change_screen (GtkWidget *widget,
		       gboolean   can_change_screen)
{
  can_change_screen = can_change_screen != FALSE;
  
Matthias Clasen's avatar
Matthias Clasen committed
335
  g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
336 337 338 339 340 341 342 343 344 345
		     GUINT_TO_POINTER (can_change_screen));
}

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

}

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

  return result;
}

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

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

392 393 394
  return result;
}

Matthias Clasen's avatar
Matthias Clasen committed
395

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

static void
gtk_drag_release_ipc_widget (GtkWidget *widget)
{
Matthias Clasen's avatar
Matthias Clasen committed
407
  GtkWindow *window = GTK_WINDOW (widget);
408 409 410
  GdkScreen *screen = gtk_widget_get_screen (widget);
  GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
					    "gtk-dnd-ipc-widgets");
Matthias Clasen's avatar
Matthias Clasen committed
411 412
  if (window->group)
    gtk_window_group_remove_window (window->group, window);
413
  drag_widgets = g_slist_prepend (drag_widgets, widget);
414
  g_object_set_data (G_OBJECT (screen),
Matthias Clasen's avatar
Matthias Clasen committed
415
		     I_("gtk-dnd-ipc-widgets"),
416
		     drag_widgets);
417 418
}

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
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;
}

456 457 458 459 460 461
static void
gtk_drag_get_event_actions (GdkEvent *event, 
			    gint button, 
			    GdkDragAction  actions,
			    GdkDragAction *suggested_action,
			    GdkDragAction *possible_actions)
462
{
463 464
  *suggested_action = 0;
  *possible_actions = 0;
Owen Taylor's avatar
Owen Taylor committed
465

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

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

531 532 533 534
	  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;
535
	  else if (actions & GDK_ACTION_MOVE)
536
	    *suggested_action = GDK_ACTION_MOVE;
537
	  else if (actions & GDK_ACTION_LINK)
538
	    *suggested_action = GDK_ACTION_LINK;
539 540
	}
    }
Owen Taylor's avatar
Owen Taylor committed
541 542 543 544 545 546 547 548 549 550 551
  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;
    }
552 553 554 555 556 557 558 559
}

static gboolean
gtk_drag_can_use_rgba_cursor (GdkDisplay *display, 
			      gint        width,
			      gint        height)
{
  guint max_width, max_height;
560
  
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  if (!gdk_display_supports_cursor_color (display))
    return FALSE;

  if (!gdk_display_supports_cursor_alpha (display))
    return FALSE;

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

  return TRUE;
577 578
}

Owen Taylor's avatar
Owen Taylor committed
579
static GdkCursor *
580 581 582
gtk_drag_get_cursor (GdkDisplay        *display,
		     GdkDragAction      action,
		     GtkDragSourceInfo *info)
583
{
Matthias Clasen's avatar
Matthias Clasen committed
584
  gint i;
585 586 587 588 589 590 591 592 593 594 595 596 597 598

  /* reconstruct the cursors for each new drag (thus !info),
   * to catch cursor theme changes 
   */ 
  if (!info)
    {
      for (i = 0 ; i < n_drag_cursors - 1; i++)
	if (drag_cursors[i].cursor != NULL)
	  {
	    gdk_cursor_unref (drag_cursors[i].cursor);
	    drag_cursors[i].cursor = NULL;
	  }
    }
 
599 600 601
  for (i = 0 ; i < n_drag_cursors - 1; i++)
    if (drag_cursors[i].action == action)
      break;
602 603 604 605 606

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

607 608
  if (drag_cursors[i].cursor != NULL)
    {
609
      if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
610 611 612 613 614
	{
	  gdk_cursor_unref (drag_cursors[i].cursor);
	  drag_cursors[i].cursor = NULL;
	}
    }
615 616 617 618
  
  if (drag_cursors[i].cursor == NULL)
    drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
  
619
  if (drag_cursors[i].cursor == NULL)
620 621 622
    drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);

  if (info && info->icon_pixbuf) 
623
    {
624 625 626 627
      gint cursor_width, cursor_height;
      gint icon_width, icon_height;
      gint width, height;
      GdkPixbuf *cursor_pixbuf, *pixbuf;
628
      gint hot_x, hot_y;
Manish Singh's avatar
Manish Singh committed
629
      gint icon_x, icon_y, ref_x, ref_y;
630 631 632 633 634 635 636 637 638

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

640 641
      icon_x = info->hot_x;
      icon_y = info->hot_y;
Matthias Clasen's avatar
Matthias Clasen committed
642 643 644 645
      icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
      icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);

      hot_x = hot_y = 0;
646 647
      cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
      if (!cursor_pixbuf)
648
	cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
649 650 651 652 653 654 655
      else
	{
	  if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
	    hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
	  
	  if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
	    hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
656

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
#if 0	  
	  /* The code below is an attempt to let cursor themes
	   * determine the attachment of the icon to enable things
	   * like the following:
	   *
	   *    +-----+
           *    |     |
           *    |     ||
           *    +-----+|
           *        ---+
           * 
           * It does not work since Xcursor doesn't allow to attach
           * any additional information to cursors in a retrievable
           * way  (there are comments, but no way to get at them
           * short of searching for the actual cursor file).
           * If this code ever gets used, the icon_window placement
           * must be changed to recognize these placement options
           * as well. Note that this code ignores info->hot_x/y.
           */ 
676 677 678
	  for (j = 0; j < 10; j++)
	    {
	      const gchar *opt;
679
	      gchar key[32];
680
	      gchar **toks;
681
	      GtkAnchorType icon_anchor;
Owen Taylor's avatar
Owen Taylor committed
682

683 684 685 686 687 688 689 690 691 692 693 694 695
	      g_snprintf (key, 32, "comment%d", j);
	      opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
	      if (opt && g_str_has_prefix ("icon-attach:", opt))
		{
		  toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
		  if (g_strv_length (toks) != 3)
		    {
		      g_strfreev (toks);
		      break;
		    }
		  icon_anchor = atoi (toks[0]);
		  icon_x = atoi (toks[1]);
		  icon_y = atoi (toks[2]);
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
		  
		  switch (icon_anchor)
		    {
		    case GTK_ANCHOR_NORTH:
		    case GTK_ANCHOR_CENTER:
		    case GTK_ANCHOR_SOUTH:
		      icon_x += icon_width / 2;
		      break;
		    case GTK_ANCHOR_NORTH_EAST:
		    case GTK_ANCHOR_EAST:
		    case GTK_ANCHOR_SOUTH_EAST:
		      icon_x += icon_width;
		      break;
		    default: ;
		    }
		  
		  switch (icon_anchor)
		    {
		    case GTK_ANCHOR_WEST:
		    case GTK_ANCHOR_CENTER:
		    case GTK_ANCHOR_EAST:
		      icon_y += icon_height / 2;
		      break;
		    case GTK_ANCHOR_SOUTH_WEST:
		    case GTK_ANCHOR_SOUTH:
		    case GTK_ANCHOR_SOUTH_EAST:
		      icon_x += icon_height;
		      break;
		    default: ;
		    }
726 727 728 729 730

		  g_strfreev (toks);
		  break;
		}
	    }
731
#endif
732
	}
Matthias Clasen's avatar
Matthias Clasen committed
733

734 735
      cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
      cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
Matthias Clasen's avatar
Matthias Clasen committed
736
      
737 738 739 740 741 742 743
      ref_x = MAX (hot_x, icon_x);
      ref_y = MAX (hot_y, icon_y);
      width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
      height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
         
      if (gtk_drag_can_use_rgba_cursor (display, width, height))
	{
744 745 746
	  /* Composite cursor and icon so that both hotspots
	   * end up at (ref_x, ref_y)
	   */
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
				   width, height); 
	  
	  gdk_pixbuf_fill (pixbuf, 0xff000000);
	  
	  gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
				ref_x - icon_x, ref_y - icon_y, 
				icon_width, icon_height,
				ref_x - icon_x, ref_y - icon_y, 
				1.0, 1.0, 
				GDK_INTERP_BILINEAR, 255);
	  
	  gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
				ref_x - hot_x, ref_y - hot_y, 
				cursor_width, cursor_height,
				ref_x - hot_x, ref_y - hot_y,
				1.0, 1.0, 
				GDK_INTERP_BILINEAR, 255);
	  
	  info->drag_cursors[i] = 
767
	    gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
768 769 770 771 772 773 774 775 776 777
	  
	  g_object_unref (pixbuf);
	}
      
      g_object_unref (cursor_pixbuf);
      
      if (info->drag_cursors[i] != NULL)
	return info->drag_cursors[i];
    }
 
778 779 780
  return drag_cursors[i].cursor;
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
static void
gtk_drag_update_cursor (GtkDragSourceInfo *info)
{
  GdkCursor *cursor;
  gint i;

  if (!info->have_grab)
    return;

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

  cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor), 
799
				drag_cursors[i].action, info);
800 801 802 803 804 805 806 807 808 809 810 811
  
  if (cursor != info->cursor)
    {
      gdk_pointer_grab (info->ipc_widget->window, FALSE,
			GDK_POINTER_MOTION_MASK |
			GDK_BUTTON_RELEASE_MASK,
			NULL,
			cursor, info->grab_time);
      info->cursor = cursor;
    }
}

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
/********************
 * 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
834

835 836 837
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
  g_return_if_fail (!context->is_source);
Owen Taylor's avatar
Owen Taylor committed
838

Matthias Clasen's avatar
Matthias Clasen committed
839
  selection_widget = gtk_drag_get_ipc_widget (widget);
Owen Taylor's avatar
Owen Taylor committed
840

Manish Singh's avatar
Manish Singh committed
841 842
  g_object_ref (context);
  g_object_ref (widget);
843
  
Manish Singh's avatar
Manish Singh committed
844 845
  g_signal_connect (selection_widget, "selection_received",
		    G_CALLBACK (gtk_drag_selection_received), widget);
Owen Taylor's avatar
Owen Taylor committed
846

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

849
  gtk_selection_convert (selection_widget,
Owen Taylor's avatar
Owen Taylor committed
850
			 gdk_drag_get_selection (context),
851 852 853 854
			 target,
			 time);
}

855 856 857 858 859 860 861 862 863 864 865 866

/*************************************************************
 * 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
867
GtkWidget *
868 869 870
gtk_drag_get_source_widget (GdkDragContext *context)
{
  GSList *tmp_list;
Owen Taylor's avatar
Owen Taylor committed
871

872 873 874
  g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
  g_return_val_if_fail (!context->is_source, NULL);
  
875 876 877 878 879 880 881 882
  tmp_list = source_widgets;
  while (tmp_list)
    {
      GtkWidget *ipc_widget = tmp_list->data;
      
      if (ipc_widget->window == context->source_window)
	{
	  GtkDragSourceInfo *info;
Manish Singh's avatar
Manish Singh committed
883
	  info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
Owen Taylor's avatar
Owen Taylor committed
884

885 886
	  return info ? info->widget : NULL;
	}
Owen Taylor's avatar
Owen Taylor committed
887

888 889
      tmp_list = tmp_list->next;
    }
Owen Taylor's avatar
Owen Taylor committed
890

891 892 893
  return NULL;
}

894 895 896 897 898 899 900 901 902 903 904 905 906 907
/*************************************************************
 * 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,
908
		 gboolean        del,
909 910 911
		 guint32         time)
{
  GdkAtom target = GDK_NONE;
Owen Taylor's avatar
Owen Taylor committed
912

913 914
  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
  g_return_if_fail (!context->is_source);
Owen Taylor's avatar
Owen Taylor committed
915

916
  if (success && del)
917
    {
918
      target = gdk_atom_intern_static_string ("DELETE");
919 920 921
    }
  else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
    {
922 923 924
      target = gdk_atom_intern_static_string (success ? 
					      "XmTRANSFER_SUCCESS" : 
					      "XmTRANSFER_FAILURE");
925
    }
Owen Taylor's avatar
Owen Taylor committed
926

927 928
  if (target != GDK_NONE)
    {
Matthias Clasen's avatar
Matthias Clasen committed
929
      GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_drawable_get_screen (context->source_window));
Owen Taylor's avatar
Owen Taylor committed
930

Manish Singh's avatar
Manish Singh committed
931
      g_object_ref (context);
932
      
Matthias Clasen's avatar
Matthias Clasen committed
933
      g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
Manish Singh's avatar
Manish Singh committed
934 935 936
      g_signal_connect (selection_widget, "selection_received",
			G_CALLBACK (gtk_drag_selection_received),
			NULL);
937 938
      
      gtk_selection_convert (selection_widget,
Owen Taylor's avatar
Owen Taylor committed
939
			     gdk_drag_get_selection (context),
940 941 942 943
			     target,
			     time);
    }
  
944
  if (!(success && del))
945 946 947 948
    gdk_drop_finish (context, success, time);
}

/*************************************************************
Owen Taylor's avatar
Owen Taylor committed
949 950
 * gtk_drag_highlight_expose:
 *     Callback for expose_event for highlighted widgets.
951 952
 *   arguments:
 *     widget:
Owen Taylor's avatar
Owen Taylor committed
953 954
 *     event:
 *     data:
955 956 957
 *   results:
 *************************************************************/

Owen Taylor's avatar
Owen Taylor committed
958 959 960 961
static gboolean
gtk_drag_highlight_expose (GtkWidget      *widget,
			   GdkEventExpose *event,
			   gpointer        data)
962
{
963
  gint x, y, width, height;
Owen Taylor's avatar
Owen Taylor committed
964
  
965
  if (GTK_WIDGET_DRAWABLE (widget))
966
    {
967 968
      cairo_t *cr;
      
969 970 971 972 973 974 975 976 977 978 979
      if (GTK_WIDGET_NO_WINDOW (widget))
	{
	  x = widget->allocation.x;
	  y = widget->allocation.y;
	  width = widget->allocation.width;
	  height = widget->allocation.height;
	}
      else
	{
	  x = 0;
	  y = 0;
Manish Singh's avatar
Manish Singh committed
980
	  gdk_drawable_get_size (widget->window, &width, &height);
981 982
	}
      
Manish Singh's avatar
Manish Singh committed
983 984
      gtk_paint_shadow (widget->style, widget->window,
		        GTK_STATE_NORMAL, GTK_SHADOW_OUT,
985
		        &event->area, widget, "dnd",
Manish Singh's avatar
Manish Singh committed
986
			x, y, width, height);
987 988 989 990 991 992 993 994 995

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

Owen Taylor's avatar
Owen Taylor committed
998
  return FALSE;
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
}

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

void 
gtk_drag_highlight (GtkWidget  *widget)
{
1012
  g_return_if_fail (GTK_IS_WIDGET (widget));
Manish Singh's avatar
Manish Singh committed
1013 1014 1015 1016

  g_signal_connect_after (widget, "expose_event",
			  G_CALLBACK (gtk_drag_highlight_expose),
			  NULL);
Owen Taylor's avatar
Owen Taylor committed
1017

1018
  gtk_widget_queue_draw (widget);
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
}

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

void 
Owen Taylor's avatar
Owen Taylor committed
1030
gtk_drag_unhighlight (GtkWidget *widget)
1031
{
1032
  g_return_if_fail (GTK_IS_WIDGET (widget));
Manish Singh's avatar
Manish Singh committed
1033 1034 1035 1036

  g_signal_handlers_disconnect_by_func (widget,
					gtk_drag_highlight_expose,
					NULL);
1037
  
Manish Singh's avatar
Manish Singh committed
1038
  gtk_widget_queue_draw (widget);
1039 1040
}

Owen Taylor's avatar
Owen Taylor committed
1041 1042 1043 1044 1045 1046 1047 1048 1049
static void
gtk_drag_dest_set_internal (GtkWidget       *widget,
			    GtkDragDestSite *site)
{
  GtkDragDestSite *old_site;
  
  g_return_if_fail (widget != NULL);

  /* HACK, do this in the destroy */
Manish Singh's avatar
Manish Singh committed
1050
  old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
Owen Taylor's avatar
Owen Taylor committed
1051
  if (old_site)
Manish Singh's avatar
Manish Singh committed
1052 1053 1054 1055 1056 1057 1058
    {
      g_signal_handlers_disconnect_by_func (widget,
					    gtk_drag_dest_realized,
					    old_site);
      g_signal_handlers_disconnect_by_func (widget,
					    gtk_drag_dest_hierarchy_changed,
					    old_site);
1059 1060

      site->track_motion = old_site->track_motion;
Manish Singh's avatar
Manish Singh committed
1061
    }
Owen Taylor's avatar
Owen Taylor committed
1062 1063 1064 1065

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

Manish Singh's avatar
Manish Singh committed
1066 1067 1068 1069
  g_signal_connect (widget, "realize",
		    G_CALLBACK (gtk_drag_dest_realized), site);
  g_signal_connect (widget, "hierarchy_changed",
		    G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
Owen Taylor's avatar
Owen Taylor committed
1070

Matthias Clasen's avatar
Matthias Clasen committed
1071
  g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
Manish Singh's avatar
Manish Singh committed
1072
			  site, gtk_drag_dest_site_destroy);
Owen Taylor's avatar
Owen Taylor committed
1073 1074
}

1075
/**
1076
 * gtk_drag_dest_set:
1077 1078 1079
 * @widget: a #GtkWidget
 * @flags: which types of default drag behavior to use
 * @targets: a pointer to an array of #GtkTargetEntry<!-- -->s indicating
1080 1081
 * the drop types that this @widget will accept. Later you can access the list
 * with gtk_drag_dest_get_target_list() and gtk_drag_dest_find_target().
1082 1083 1084 1085 1086 1087 1088
 * @n_targets: the number of entries in @targets.
 * @actions: a bitmask of possible actions for a drop onto this @widget.
 *
 * Sets a widget as a potential drop destination, and adds default behaviors.
 *
 * The default behaviors listed in @flags have an effect similar
 * to installing default handlers for the widget's drag-and-drop signals
1089 1090 1091 1092
 * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
 * sufficient to connect to the widget's #GtkWidget::drag-data-received
 * signal to get primitive, but consistent drag-and-drop support.
1093
 *
1094 1095 1096 1097 1098 1099 1100 1101
 * Things become more complicated when you try to preview the dragged data,
 * as described in the documentation for #GtkWidget:drag-motion. The default
 * behaviors described by @flags make some assumptions, that can conflict
 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
 * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
 * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
 * Especially the later is dramatic, when your own #GtkWidget:drag-motion
 * handler calls gtk_drag_get_data() to inspect the dragged data.
1102 1103 1104 1105 1106 1107 1108
 */
void
gtk_drag_dest_set (GtkWidget            *widget,
		   GtkDestDefaults       flags,
		   const GtkTargetEntry *targets,
		   gint                  n_targets,
		   GdkDragAction         actions)
1109 1110 1111
{
  GtkDragDestSite *site;
  
1112
  g_return_if_fail (GTK_IS_WIDGET (widget));
Owen Taylor's avatar
Owen Taylor committed
1113

1114
  site = g_new (GtkDragDestSite, 1);
Owen Taylor's avatar
Owen Taylor committed
1115

1116 1117
  site->flags = flags;
  site->have_drag = FALSE;
1118 1119 1120 1121
  if (targets)
    site->target_list = gtk_target_list_new (targets, n_targets);
  else
    site->target_list = NULL;
1122
  site->actions = actions;
1123
  site->do_proxy = FALSE;
1124
  site->proxy_window = NULL;
1125
  site->track_motion = FALSE;
Owen Taylor's avatar
Owen Taylor committed
1126

Owen Taylor's avatar
Owen Taylor committed
1127
  gtk_drag_dest_set_internal (widget, site);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
}

/*************************************************************
 * 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;
  
1151 1152
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
Owen Taylor's avatar
Owen Taylor committed
1153

1154
  site = g_new (GtkDragDestSite, 1);
Owen Taylor's avatar
Owen Taylor committed
1155

1156 1157 1158 1159 1160
  site->flags = 0;
  site->have_drag = FALSE;
  site->target_list = NULL;
  site->actions = 0;
  site->proxy_window = proxy_window;
1161
  if (proxy_window)
Manish Singh's avatar
Manish Singh committed
1162
    g_object_ref (proxy_window);
1163
  site->do_proxy = TRUE;
1164 1165
  site->proxy_protocol = protocol;
  site->proxy_coords = use_coordinates;
1166
  site->track_motion = FALSE;
Owen Taylor's avatar
Owen Taylor committed
1167

Owen Taylor's avatar
Owen Taylor committed
1168
  gtk_drag_dest_set_internal (widget, site);
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
}

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

void 
Owen Taylor's avatar
Owen Taylor committed
1180
gtk_drag_dest_unset (GtkWidget *widget)
1181
{
1182
  g_return_if_fail (GTK_IS_WIDGET (widget));
Owen Taylor's avatar
Owen Taylor committed
1183

Matthias Clasen's avatar
Matthias Clasen committed
1184
  g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1185 1186
}

Havoc Pennington's avatar
Havoc Pennington committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
/**
 * 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;
1200 1201

  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1202
  
Manish Singh's avatar
Manish Singh committed
1203
  site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
Havoc Pennington's avatar
Havoc Pennington committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

  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;
Manish Singh's avatar
Manish Singh committed
1222

1223 1224
  g_return_if_fail (GTK_IS_WIDGET (widget));
  
Manish Singh's avatar
Manish Singh committed
1225
  site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
Havoc Pennington's avatar
Havoc Pennington committed
1226
  
1227
  if (!site)
Havoc Pennington's avatar
Havoc Pennington committed
1228
    {
1229 1230
      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");
Havoc Pennington's avatar
Havoc Pennington committed
1231 1232 1233 1234
      return;
    }

  if (target_list)
1235
    gtk_target_list_ref (target_list);
Havoc Pennington's avatar
Havoc Pennington committed
1236 1237 1238 1239 1240 1241 1242
  
  if (site->target_list)
    gtk_target_list_unref (site->target_list);

  site->target_list = target_list;
}

1243 1244 1245 1246 1247
/**
 * gtk_drag_dest_add_text_targets:
 * @widget: a #GtkWidget that's a drag destination
 *
 * Add the text targets supported by #GtkSelection to
1248 1249 1250 1251
 * the target list of the drag destination. The targets
 * are added with @info = 0. If you need another value, 
 * use gtk_target_list_add_text_targets() and
 * gtk_drag_dest_set_target_list().
1252 1253 1254 1255 1256 1257 1258 1259 1260
 * 
 * Since: 2.6
 **/
void
gtk_drag_dest_add_text_targets (GtkWidget *widget)
{
  GtkTargetList *target_list;

  target_list = gtk_drag_dest_get_target_list (widget);
1261 1262 1263
  if (target_list)
    gtk_target_list_ref (target_list);
  else
1264
    target_list = gtk_target_list_new (NULL, 0);
1265
  gtk_target_list_add_text_targets (target_list, 0);
1266
  gtk_drag_dest_set_target_list (widget, target_list);
1267
  gtk_target_list_unref (target_list);
1268
}
Havoc Pennington's avatar
Havoc Pennington committed
1269

1270 1271 1272 1273 1274
/**
 * gtk_drag_dest_add_image_targets:
 * @widget: a #GtkWidget that's a drag destination
 *
 * Add the image targets supported by #GtkSelection to
1275 1276 1277 1278
 * the target list of the drag destination. The targets
 * are added with @info = 0. If you need another value, 
 * use gtk_target_list_add_image_targets() and
 * gtk_drag_dest_set_target_list().
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
 * 
 * Since: 2.6
 **/
void
gtk_drag_dest_add_image_targets (GtkWidget *widget)
{
  GtkTargetList *target_list;

  target_list = gtk_drag_dest_get_target_list (widget);
  if (target_list)
    gtk_target_list_ref (target_list);
  else
    target_list = gtk_target_list_new (NULL, 0);
1292
  gtk_target_list_add_image_targets (target_list, 0, FALSE);
1293 1294 1295 1296
  gtk_drag_dest_set_target_list (widget, target_list);
  gtk_target_list_unref (target_list);
}

Matthias Clasen's avatar
Matthias Clasen committed
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
/**
 * gtk_drag_dest_add_uri_targets:
 * @widget: a #GtkWidget that's a drag destination
 *
 * Add the URI targets supported by #GtkSelection to
 * the target list of the drag destination. The targets
 * are added with @info = 0. If you need another value, 
 * use gtk_target_list_add_uri_targets() and
 * gtk_drag_dest_set_target_list().
 * 
 * Since: 2.6
 **/
void
gtk_drag_dest_add_uri_targets (GtkWidget *widget)
{
  GtkTargetList *target_list;

  target_list = gtk_drag_dest_get_target_list (widget);
  if (target_list)
    gtk_target_list_ref (target_list);
  else
    target_list = gtk_target_list_new (NULL, 0);
  gtk_target_list_add_uri_targets (target_list, 0);
  gtk_drag_dest_set_target_list (widget, target_list);
  gtk_target_list_unref (target_list);
}

1324 1325 1326 1327 1328 1329 1330 1331 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 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
/**
 * gtk_drag_dest_set_track_motion:
 * @widget: a #GtkWidget that's a drag destination
 * @track_motion: whether to accept all targets