gtkdnd.c 91.8 KB
Newer Older
1
/* GTK - The GIMP Toolkit
2
 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 4
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18 19
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

27 28
#include "gdkconfig.h"

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

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

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

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

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

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

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

73 74 75 76
  /* Stored button press information to detect drag beginning */
  gint               state;
  gint               x, y;
};
Owen Taylor's avatar
Owen Taylor committed
77
  
Owen Taylor's avatar
Owen Taylor committed
78 79
struct _GtkDragSourceInfo 
{
80 81
  GtkWidget         *widget;
  GtkTargetList     *target_list; /* Targets for drag data */
82
  GdkDragAction      possible_actions; /* Actions allowed by source */
83 84
  GdkDragContext    *context;	  /* drag context */
  GtkWidget         *icon_window; /* Window for drag */
85
  GtkWidget         *fallback_icon; /* Window for drag used on other screens */
86 87 88 89
  GtkWidget         *ipc_widget;  /* GtkInvisible for grab, message passing */
  GdkCursor         *cursor;	  /* Cursor for drag */
  gint hot_x, hot_y;		  /* Hot spot for drag */
  gint button;			  /* mouse button starting drag */
Owen Taylor's avatar
Owen Taylor committed
90

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

94 95
  gint               start_x, start_y; /* Initial position */
  gint               cur_x, cur_y;     /* Current Position */
96
  GdkScreen         *cur_screen;       /* Current screen for pointer */
Owen Taylor's avatar
Owen Taylor committed
97

98
  guint32            grab_time;   /* timestamp for initial grab */
99 100 101
  GList             *selections;  /* selections we've claimed */
  
  GtkDragDestInfo   *proxy_dest;  /* Set if this is a proxy drag */
Owen Taylor's avatar
Owen Taylor committed
102

Owen Taylor's avatar
Owen Taylor committed
103 104
  guint              drop_timeout;     /* Timeout for aborting drop */
  guint              destroy_icon : 1; /* If true, destroy icon_window
105
      				        */
106
  guint              have_grab : 1;    /* Do we still have the pointer grab
107
					 */
108 109
};

Owen Taylor's avatar
Owen Taylor committed
110 111
struct _GtkDragDestSite 
{
112 113 114 115 116
  GtkDestDefaults    flags;
  GtkTargetList     *target_list;
  GdkDragAction      actions;
  GdkWindow         *proxy_window;
  GdkDragProtocol    proxy_protocol;
117
  gboolean           do_proxy : 1;
118 119 120
  gboolean           proxy_coords : 1;
  gboolean           have_drag : 1;
};
Owen Taylor's avatar
Owen Taylor committed
121
  
Owen Taylor's avatar
Owen Taylor committed
122 123
struct _GtkDragDestInfo 
{
124 125 126 127 128 129 130 131 132 133 134 135 136
  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 */
};

137
#define DROP_ABORT_TIME 300000
138 139 140 141 142 143

#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
144 145
struct _GtkDragAnim 
{
146 147 148 149 150
  GtkDragSourceInfo *info;
  gint step;
  gint n_steps;
};

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

Owen Taylor's avatar
Owen Taylor committed
191 192 193
static gboolean      gtk_drag_highlight_expose   (GtkWidget      *widget,
					  	  GdkEventExpose *event,
						  gpointer        data);
194

195 196 197 198 199 200 201 202 203 204 205
static void     gtk_drag_selection_received     (GtkWidget        *widget,
						 GtkSelectionData *selection_data,
						 guint32           time,
						 gpointer          data);
static void     gtk_drag_find_widget            (GtkWidget        *widget,
						 GtkDragFindData  *data);
static void     gtk_drag_proxy_begin            (GtkWidget        *widget,
						 GtkDragDestInfo  *dest_info,
						 guint32           time);
static void     gtk_drag_dest_realized          (GtkWidget        *widget);
static void     gtk_drag_dest_hierarchy_changed (GtkWidget        *widget,
Owen Taylor's avatar
Owen Taylor committed
206
						 GtkWidget        *previous_toplevel);
207 208 209 210 211 212 213 214 215 216 217 218 219 220
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
221 222 223 224 225 226

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);
227 228 229 230 231 232 233 234 235 236 237

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);
238 239
static void gtk_drag_cancel                    (GtkDragSourceInfo *info,
						guint32            time);
240 241 242 243 244 245 246 247 248 249 250 251

static gint gtk_drag_source_event_cb           (GtkWidget         *widget,
						GdkEvent          *event,
						gpointer           data);
static void gtk_drag_source_site_destroy       (gpointer           data);
static void gtk_drag_selection_get             (GtkWidget         *widget, 
						GtkSelectionData  *selection_data,
						guint              sel_info,
						guint32            time,
						gpointer           data);
static gint gtk_drag_anim_timeout              (gpointer           data);
static void gtk_drag_remove_icon               (GtkDragSourceInfo *info);
Owen Taylor's avatar
Owen Taylor committed
252
static void gtk_drag_source_info_destroy       (GtkDragSourceInfo *info);
253
static void gtk_drag_update                    (GtkDragSourceInfo *info,
254
						GdkScreen         *screen,
255 256 257
						gint               x_root,
						gint               y_root,
						GdkEvent          *event);
258 259 260
static gint gtk_drag_motion_cb                 (GtkWidget         *widget, 
					        GdkEventMotion    *event, 
					        gpointer           data);
261 262 263
static gint gtk_drag_key_cb                    (GtkWidget         *widget, 
					        GdkEventKey       *event, 
					        gpointer           data);
264 265 266 267 268 269 270 271 272 273 274
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
275
static const guchar action_ask_bits[] = {
276 277 278 279 280 281
  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
282
static const guchar action_ask_mask_bits[] = {
283 284 285 286 287 288
  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
289
static const guchar action_copy_bits[] = {
290 291 292 293 294 295
  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
296
static const guchar action_copy_mask_bits[] = {
297 298 299 300 301 302
  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
303
static const guchar action_move_bits[] = {
304 305 306 307 308 309
  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
310
static const guchar action_move_mask_bits[] = {
311 312 313 314 315 316
  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
317
static const guchar action_link_bits[] = {
318 319 320 321 322 323
  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
324
static const guchar action_link_mask_bits[] = {
325 326 327 328 329 330
  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
331
static const guchar action_none_bits[] = {
332 333 334 335 336 337
  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
338
static const guchar action_none_mask_bits[] = {
339 340 341 342 343 344 345
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x0f, 0x00, 0x0f, 0x00, 
  0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x00, 0x0f, 0x00, 
  0x0f, 0x00, 0x0f, 0x00, 0x07, 0x00, 0x07, 0x00, };

#define CURSOR_WIDTH 16
#define CURSOR_HEIGHT 16

Owen Taylor's avatar
Owen Taylor committed
346
static struct {
347
  GdkDragAction action;
348 349
  const guchar *bits;
  const guchar *mask;
350 351 352 353 354 355 356 357 358 359
  GdkCursor    *cursor;
} drag_cursors[] = {
  { GDK_ACTION_DEFAULT, 0 },
  { GDK_ACTION_ASK,   action_ask_bits,  action_ask_mask_bits,  NULL },
  { GDK_ACTION_COPY,  action_copy_bits, action_copy_mask_bits, NULL },
  { GDK_ACTION_MOVE,  action_move_bits, action_move_mask_bits, NULL },
  { GDK_ACTION_LINK,  action_link_bits, action_link_mask_bits, NULL },
  { 0              ,  action_none_bits, action_none_mask_bits, NULL },
};

Owen Taylor's avatar
Owen Taylor committed
360
static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
361 362 363 364 365

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

Manish Singh's avatar
Manish Singh committed
366
static void
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
set_can_change_screen (GtkWidget *widget,
		       gboolean   can_change_screen)
{
  can_change_screen = can_change_screen != FALSE;
  
  g_object_set_data (G_OBJECT (widget), "gtk-dnd-can-change-screen",
		     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;

}

383 384 385 386 387 388 389 390 391
/*************************************************************
 * gtk_drag_get_ipc_widget:
 *     Return a invisible, off-screen, override-redirect
 *     widget for IPC.
 *   arguments:
 *     
 *   results:
 *************************************************************/

Owen Taylor's avatar
Owen Taylor committed
392
static GtkWidget *
393
gtk_drag_get_ipc_widget (GdkScreen *screen)
394 395
{
  GtkWidget *result;
396 397
  GSList *drag_widgets = g_object_get_data (G_OBJECT (screen), 
					    "gtk-dnd-ipc-widgets");
398 399 400 401 402 403
  
  if (drag_widgets)
    {
      GSList *tmp = drag_widgets;
      result = drag_widgets->data;
      drag_widgets = drag_widgets->next;
404 405 406
      g_object_set_data (G_OBJECT (screen),
			 "gtk-dnd-ipc-widgets",
			 drag_widgets);
407 408 409 410
      g_slist_free_1 (tmp);
    }
  else
    {
411
      result = gtk_invisible_new_for_screen (screen);
412 413
      gtk_widget_show (result);
    }
Owen Taylor's avatar
Owen Taylor committed
414

415 416 417
  return result;
}

Owen Taylor's avatar
Owen Taylor committed
418
/***************************************************************
419
 * gtk_drag_release_ipc_widget:
Owen Taylor's avatar
Owen Taylor committed
420
 *     Releases widget retrieved with gtk_drag_get_ipc_widget ()
421 422 423
 *   arguments:
 *     widget: the widget to release.
 *   results:
Owen Taylor's avatar
Owen Taylor committed
424
 ***************************************************************/
425 426 427 428

static void
gtk_drag_release_ipc_widget (GtkWidget *widget)
{
429 430 431
  GdkScreen *screen = gtk_widget_get_screen (widget);
  GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
					    "gtk-dnd-ipc-widgets");
432
  drag_widgets = g_slist_prepend (drag_widgets, widget);
433 434 435
  g_object_set_data (G_OBJECT (screen),
		     "gtk-dnd-ipc-widgets",
		     drag_widgets);
436 437
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
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;
}

475 476 477 478 479 480
static void
gtk_drag_get_event_actions (GdkEvent *event, 
			    gint button, 
			    GdkDragAction  actions,
			    GdkDragAction *suggested_action,
			    GdkDragAction *possible_actions)
481
{
482 483
  *suggested_action = 0;
  *possible_actions = 0;
Owen Taylor's avatar
Owen Taylor committed
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  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
504 505
	case GDK_ENTER_NOTIFY:
	case GDK_LEAVE_NOTIFY:
506 507
	  state = event->crossing.state;
	  break;
Owen Taylor's avatar
Owen Taylor committed
508 509 510
	default:
	  break;
	}
511

Owen Taylor's avatar
Owen Taylor committed
512
      if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
513 514 515 516 517
	{
	  *suggested_action = GDK_ACTION_ASK;
	  *possible_actions = actions;
	}
      else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
518 519
	{
	  if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
520 521 522 523 524 525 526
	    {
	      if (actions & GDK_ACTION_LINK)
		{
		  *suggested_action = GDK_ACTION_LINK;
		  *possible_actions = GDK_ACTION_LINK;
		}
	    }
527
	  else if (state & GDK_CONTROL_MASK)
528 529 530 531 532 533 534 535
	    {
	      if (actions & GDK_ACTION_COPY)
		{
		  *suggested_action = GDK_ACTION_COPY;
		  *possible_actions = GDK_ACTION_COPY;
		}
	      return;
	    }
536
	  else
537 538 539 540 541 542 543 544
	    {
	      if (actions & GDK_ACTION_MOVE)
		{
		  *suggested_action = GDK_ACTION_MOVE;
		  *possible_actions = GDK_ACTION_MOVE;
		}
	      return;
	    }
545 546 547
	}
      else
	{
548
	  *possible_actions = actions;
Owen Taylor's avatar
Owen Taylor committed
549

550 551 552 553
	  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;
554
	  else if (actions & GDK_ACTION_MOVE)
555
	    *suggested_action = GDK_ACTION_MOVE;
556
	  else if (actions & GDK_ACTION_LINK)
557
	    *suggested_action = GDK_ACTION_LINK;
558 559
	}
    }
Owen Taylor's avatar
Owen Taylor committed
560 561 562 563 564 565 566 567 568 569 570
  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;
    }
571
  
572
  return;
573 574
}

Owen Taylor's avatar
Owen Taylor committed
575
static GdkCursor *
576
gtk_drag_get_cursor (GdkDisplay   *display,
577
		     GdkDragAction action)
578 579 580 581 582 583
{
  gint i;
  
  for (i = 0 ; i < n_drag_cursors - 1; i++)
    if (drag_cursors[i].action == action)
      break;
584 585
  if (drag_cursors[i].cursor != NULL)
    {
586
      if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
587 588 589 590 591
	{
	  gdk_cursor_unref (drag_cursors[i].cursor);
	  drag_cursors[i].cursor = NULL;
	}
    }
Owen Taylor's avatar
Owen Taylor committed
592

593 594
  if (drag_cursors[i].cursor == NULL)
    {
595 596 597 598
      GdkColor bg = { 0, 0xffff, 0xffff, 0xffff };
      GdkColor fg = { 0, 0x0000, 0x0000, 0x0000 };
      GdkScreen *screen = gdk_display_get_default_screen (display);
      GdkWindow *window = gdk_screen_get_root_window (screen);
Owen Taylor's avatar
Owen Taylor committed
599

600
      GdkPixmap *pixmap = 
601
	gdk_bitmap_create_from_data (window, (gchar *)drag_cursors[i].bits, CURSOR_WIDTH, CURSOR_HEIGHT);
602

603
      GdkPixmap *mask = 
604
	gdk_bitmap_create_from_data (window, (gchar *)drag_cursors[i].mask, CURSOR_WIDTH, CURSOR_HEIGHT);
Owen Taylor's avatar
Owen Taylor committed
605

606
      drag_cursors[i].cursor = gdk_cursor_new_from_pixmap (pixmap, mask, &fg, &bg, 0, 0);
Owen Taylor's avatar
Owen Taylor committed
607

Manish Singh's avatar
Manish Singh committed
608 609
      g_object_unref (pixmap);
      g_object_unref (mask);
610
    }
Owen Taylor's avatar
Owen Taylor committed
611

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
  return drag_cursors[i].cursor;
}

/********************
 * Destination side *
 ********************/

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

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

638 639 640
  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
641

642
  selection_widget = gtk_drag_get_ipc_widget (gtk_widget_get_screen (widget));
Owen Taylor's avatar
Owen Taylor committed
643

Manish Singh's avatar
Manish Singh committed
644 645
  g_object_ref (context);
  g_object_ref (widget);
646
  
Manish Singh's avatar
Manish Singh committed
647 648
  g_signal_connect (selection_widget, "selection_received",
		    G_CALLBACK (gtk_drag_selection_received), widget);
Owen Taylor's avatar
Owen Taylor committed
649

Manish Singh's avatar
Manish Singh committed
650
  g_object_set_data (G_OBJECT (selection_widget), "drag-context", context);
Owen Taylor's avatar
Owen Taylor committed
651

652
  gtk_selection_convert (selection_widget,
Owen Taylor's avatar
Owen Taylor committed
653
			 gdk_drag_get_selection (context),
654 655 656 657
			 target,
			 time);
}

658 659 660 661 662 663 664 665 666 667 668 669

/*************************************************************
 * 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
670
GtkWidget *
671 672 673
gtk_drag_get_source_widget (GdkDragContext *context)
{
  GSList *tmp_list;
Owen Taylor's avatar
Owen Taylor committed
674

675 676 677
  g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
  g_return_val_if_fail (!context->is_source, NULL);
  
678 679 680 681 682 683 684 685
  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
686
	  info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
Owen Taylor's avatar
Owen Taylor committed
687

688 689
	  return info ? info->widget : NULL;
	}
Owen Taylor's avatar
Owen Taylor committed
690

691 692
      tmp_list = tmp_list->next;
    }
Owen Taylor's avatar
Owen Taylor committed
693

694 695 696
  return NULL;
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710
/*************************************************************
 * 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,
711
		 gboolean        del,
712 713 714
		 guint32         time)
{
  GdkAtom target = GDK_NONE;
Owen Taylor's avatar
Owen Taylor committed
715

716 717
  g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
  g_return_if_fail (!context->is_source);
Owen Taylor's avatar
Owen Taylor committed
718

719
  if (success && del)
720 721 722 723 724 725
    {
      target = gdk_atom_intern ("DELETE", FALSE);
    }
  else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
    {
      target = gdk_atom_intern (success ? 
Owen Taylor's avatar
Owen Taylor committed
726 727
				  "XmTRANSFER_SUCCESS" : 
				  "XmTRANSFER_FAILURE",
728 729
				FALSE);
    }
Owen Taylor's avatar
Owen Taylor committed
730

731 732
  if (target != GDK_NONE)
    {
733
      GtkWidget *selection_widget = gtk_drag_get_ipc_widget (gdk_drawable_get_screen (context->source_window));
Owen Taylor's avatar
Owen Taylor committed
734

Manish Singh's avatar
Manish Singh committed
735
      g_object_ref (context);
736
      
Manish Singh's avatar
Manish Singh committed
737 738 739 740
      g_object_set_data (G_OBJECT (selection_widget), "drag-context", context);
      g_signal_connect (selection_widget, "selection_received",
			G_CALLBACK (gtk_drag_selection_received),
			NULL);
741 742
      
      gtk_selection_convert (selection_widget,
Owen Taylor's avatar
Owen Taylor committed
743
			     gdk_drag_get_selection (context),
744 745 746 747
			     target,
			     time);
    }
  
748
  if (!(success && del))
749 750 751 752
    gdk_drop_finish (context, success, time);
}

/*************************************************************
Owen Taylor's avatar
Owen Taylor committed
753 754
 * gtk_drag_highlight_expose:
 *     Callback for expose_event for highlighted widgets.
755 756
 *   arguments:
 *     widget:
Owen Taylor's avatar
Owen Taylor committed
757 758
 *     event:
 *     data:
759 760 761
 *   results:
 *************************************************************/

Owen Taylor's avatar
Owen Taylor committed
762 763 764 765
static gboolean
gtk_drag_highlight_expose (GtkWidget      *widget,
			   GdkEventExpose *event,
			   gpointer        data)
766
{
767
  gint x, y, width, height;
Owen Taylor's avatar
Owen Taylor committed
768
  
769
  if (GTK_WIDGET_DRAWABLE (widget))
770
    {
771 772 773 774 775 776 777 778 779 780 781
      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
782
	  gdk_drawable_get_size (widget->window, &width, &height);
783 784
	}
      
Manish Singh's avatar
Manish Singh committed
785 786 787 788
      gtk_paint_shadow (widget->style, widget->window,
		        GTK_STATE_NORMAL, GTK_SHADOW_OUT,
		        NULL, widget, "dnd",
			x, y, width, height);
789 790 791 792 793
      
      gdk_draw_rectangle (widget->window,
			  widget->style->black_gc,
			  FALSE,
			  x, y, width - 1, height - 1);
794
    }
795

Owen Taylor's avatar
Owen Taylor committed
796
  return FALSE;
797 798 799 800 801 802 803 804 805 806 807 808 809
}

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

void 
gtk_drag_highlight (GtkWidget  *widget)
{
810
  g_return_if_fail (GTK_IS_WIDGET (widget));
Manish Singh's avatar
Manish Singh committed
811 812 813 814

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

816
  gtk_widget_queue_draw (widget);
817 818 819 820 821 822 823 824 825 826 827
}

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

void 
Owen Taylor's avatar
Owen Taylor committed
828
gtk_drag_unhighlight (GtkWidget *widget)
829
{
830
  g_return_if_fail (GTK_IS_WIDGET (widget));
Manish Singh's avatar
Manish Singh committed
831 832 833 834

  g_signal_handlers_disconnect_by_func (widget,
					gtk_drag_highlight_expose,
					NULL);
835
  
Manish Singh's avatar
Manish Singh committed
836
  gtk_widget_queue_draw (widget);
837 838
}

Owen Taylor's avatar
Owen Taylor committed
839 840 841 842 843 844 845 846 847
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
848
  old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
Owen Taylor's avatar
Owen Taylor committed
849
  if (old_site)
Manish Singh's avatar
Manish Singh committed
850 851 852 853 854 855 856 857
    {
      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);
    }
Owen Taylor's avatar
Owen Taylor committed
858 859 860 861

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

Manish Singh's avatar
Manish Singh committed
862 863 864 865
  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
866

Manish Singh's avatar
Manish Singh committed
867 868
  g_object_set_data_full (G_OBJECT (widget), "gtk-drag-dest",
			  site, gtk_drag_dest_site_destroy);
Owen Taylor's avatar
Owen Taylor committed
869 870 871
}
			    

872 873 874 875 876 877 878 879 880 881 882 883 884
/*************************************************************
 * 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 
885 886 887 888 889
gtk_drag_dest_set   (GtkWidget            *widget,
		     GtkDestDefaults       flags,
		     const GtkTargetEntry *targets,
		     gint                  n_targets,
		     GdkDragAction         actions)
890 891 892
{
  GtkDragDestSite *site;
  
893
  g_return_if_fail (GTK_IS_WIDGET (widget));
Owen Taylor's avatar
Owen Taylor committed
894

895
  site = g_new (GtkDragDestSite, 1);
Owen Taylor's avatar
Owen Taylor committed
896

897 898 899 900 901 902 903
  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;
904
  site->do_proxy = FALSE;
905
  site->proxy_window = NULL;
Owen Taylor's avatar
Owen Taylor committed
906

Owen Taylor's avatar
Owen Taylor committed
907
  gtk_drag_dest_set_internal (widget, site);
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
}

/*************************************************************
 * 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;
  
931 932
  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
933

934
  site = g_new (GtkDragDestSite, 1);
Owen Taylor's avatar
Owen Taylor committed
935

936 937 938 939 940
  site->flags = 0;
  site->have_drag = FALSE;
  site->target_list = NULL;
  site->actions = 0;
  site->proxy_window = proxy_window;
941
  if (proxy_window)
Manish Singh's avatar
Manish Singh committed
942
    g_object_ref (proxy_window);
943
  site->do_proxy = TRUE;
944 945
  site->proxy_protocol = protocol;
  site->proxy_coords = use_coordinates;
Owen Taylor's avatar
Owen Taylor committed
946

Owen Taylor's avatar
Owen Taylor committed
947
  gtk_drag_dest_set_internal (widget, site);
948 949 950 951 952 953 954 955 956 957 958
}

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

void 
Owen Taylor's avatar
Owen Taylor committed
959
gtk_drag_dest_unset (GtkWidget *widget)
960
{
961
  g_return_if_fail (GTK_IS_WIDGET (widget));
Owen Taylor's avatar
Owen Taylor committed
962

Manish Singh's avatar
Manish Singh committed
963
  g_object_set_data (G_OBJECT (widget), "gtk-drag-dest", NULL);
964 965
}

Havoc Pennington's avatar
Havoc Pennington committed
966 967 968 969 970 971 972 973 974 975 976 977 978
/**
 * 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;
979 980

  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
Havoc Pennington's avatar
Havoc Pennington committed
981
  
Manish Singh's avatar
Manish Singh committed
982
  site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
Havoc Pennington's avatar
Havoc Pennington committed
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000

  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
1001

1002 1003
  g_return_if_fail (GTK_IS_WIDGET (widget));
  
Manish Singh's avatar
Manish Singh committed
1004
  site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
Havoc Pennington's avatar
Havoc Pennington committed
1005 1006 1007 1008 1009 1010 1011 1012
  
  if (site == NULL)
    {
      g_warning ("can't set a target list on a widget until you've called gtk_drag_dest_set() to make the widget into a drag destination");
      return;
    }

  if (target_list)
1013
    gtk_target_list_ref (target_list);
Havoc Pennington's avatar
Havoc Pennington committed
1014 1015 1016 1017 1018 1019 1020 1021
  
  if (site->target_list)
    gtk_target_list_unref (site->target_list);

  site->target_list = target_list;
}


1022
/*************************************************************
1023
 * _gtk_drag_dest_handle_event:
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
 *     Called from widget event handling code on Drag events
 *     for destinations.
 *
 *   arguments:
 *     toplevel: Toplevel widget that received the event
 *     event:
 *   results:
 *************************************************************/

void
1034
_gtk_drag_dest_handle_event (GtkWidget *toplevel,
1035 1036 1037 1038
			    GdkEvent  *event)
{
  GtkDragDestInfo *info;
  GdkDragContext *context;
Owen Taylor's avatar
Owen Taylor committed
1039

1040 1041
  g_return_if_fail (toplevel != NULL);
  g_return_if_fail (event != NULL);
Owen Taylor's avatar
Owen Taylor committed
1042

1043
  context = event->dnd.context;
Owen Taylor's avatar
Owen Taylor committed
1044

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