Debian Codegraph @

gdk/win32/gdkdnd-win32.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 2001 Archaeopteryx Software Inc.
4 * Copyright (C) 1998-2002 Tor Lillqvist
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 */
26
27 #include "config.h"
28 #include <string.h>
29
30 #include <io.h>
31 #include <fcntl.h>
32
33 /*
34 * Support for OLE-2 drag and drop added at Archaeopteryx Software, 2001
35 * For more information, contact Stephan R.A. Deibel ([email protected])
36 *
37 * Notes on the implementation:
38 *
39 * Source drag context, IDragSource and IDataObject for it are created
40 * (almost) simultaneously, whereas target drag context and IDropTarget
41 * are separated in time - IDropTarget is created when a window is made
42 * to accept drops, while target drag context is created when a dragging
43 * cursor enters the window and is destroyed when that cursor leaves
44 * the window.
45 *
46 * There's a mismatch between data types supported by W32 (W32 formats)
47 * and by GTK+ (GDK targets).
48 * To account for it the data is transmuted back and forth. There are two
49 * main points of transmutation:
50 * * GDK convert selection: transmute W32 data to GTK+ data
51 * * GDK window property change: transmute GTK+ data to W32 data
52 *
53 * There are also two points where data formats are considered:
54 * * When source drag context is created, it gets a list of GTK+ targets
55 * that it supports, these are matched to the W32 formats they
56 * correspond to (possibly with transmutation). New W32 formats for
57 * GTK+-specific formats are also created here (see below).
58 * * When target drag context is created, it queries the IDataObject
59 * for the list of W32 formats it supports and matches these to
60 * corresponding GTK+ formats that it will be able to provide
61 * (possibly with transmutation) later. Missing GDK targets for
62 * W32-specific formats are also created here (see below).
63 *
64 * W32 formats and GTK+ targets are both integers (CLIPFORMAT and GdkAtom
65 * respectively), but cannot be used interchangeably.
66 *
67 * To accommodate advanced GTK+ applications the code allows them to
68 * register drop targets that accept W32 data formats, and to register
69 * drag sources that provide W32 data formats. To do that they must
70 * register either with the string name of the format in question
71 * (for example, "Shell IDList Array") or, for unnamed pre-defined
72 * formats, register with the stringified constant name of the format
73 * in question (for example, "CF_UNICODETEXT").
74 * If such target format is accepted/provided, GDK will not try to
75 * transmute it to/from something else. Otherwise GDK will do the following
76 * transmutation:
77 * * If GTK+ application provides image/png, image/gif or image/jpeg,
78 * GDK will claim to also provide "PNG", "GIF" or "JFIF" respectively,
79 * and will pass these along verbatim.
80 * * If GTK+ application provides any GdkPixbuf-compatible target,
81 * GDK will also offer "PNG" and CF_DIB W32 formats.
82 * * If GTK+ application provides UTF8_STRING, GDK will also offer
83 * CF_UNICODETEXT (UTF-16-encoded) and CF_TEXT (encoded with thread-
84 * and locale-depenant codepage), and will do the conversion when such
85 * data is requested.
86 * * If GTK+ application accepts image/png, image/gif or image/jpeg,
87 * GDK will claim to also accept "PNG", "GIF" or "JFIF" respectively,
88 * and will pass these along verbatim.
89 * * If GTK+ application accepts image/bmp, GDK will
90 * claim to accept CF_DIB W32 format, and will convert
91 * it, changing the header, when such data is provided.
92 * * If GTK+ application accepts UTF8_STRING, GDK will
93 * claim to accept CF_UNICODETEXT and CF_TEXT, and will do
94 * the conversion when such data is provided.
95 * * If GTK+ application accepts text/uri-list, GDK will
96 * claim to accept "Shell IDList Array", and will do the
97 * conversion when such data is provided.
98 *
99 * Currently the conversion from text/uri-list to Shell IDList Array is not
100 * implemented, so it's not possible to drag & drop files from GTK+
101 * applications to non-GTK+ applications the same way one can drag files
102 * from Windows Explorer.
103 *
104 * To accommodate GTK+ application compaibility the code allows
105 * GTK+ applications to register drop targets that accept GTK+-specific
106 * data formats, and to register drag sources that provide GTK+-specific
107 * data formats. This is done by simply registering target atom names
108 * as clipboard formats. This way two GTK+ applications can exchange
109 * data in their native formats (both well-known ones, such as UTF8_STRING,
110 * and special, known only to specific applications). This will work just
111 * fine as long as both applications agree on what kind of data is stored
112 * under such format exactly.
113 *
114 * Note that clipboard format space is limited, there can only be 16384
115 * of them for a particular user session. Therefore it is highly inadvisable
116 * to create and register such formats out of the whole cloth, dynamically.
117 * If more flexibility is needed, register one format that has some
118 * internal indicators of the kind of data it contains, then write the application
119 * in such a way that it requests the data and inspects its header before deciding
120 * whether to accept it or not. For details see GTK+ drag & drop documentation
121 * on the "drag-motion" and "drag-data-received" signals.
122 */
123
124 /* The mingw.org compiler does not export GUIDS in it's import library. To work
125 * around that, define INITGUID to have the GUIDS declared. */
126 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
127 #define INITGUID
128 #endif
129
130 /* For C-style COM wrapper macros */
131 #define COBJMACROS
132
133 #include "gdkdnd.h"
134 #include "gdkproperty.h"
135 #include "gdkinternals.h"
136 #include "gdkprivate-win32.h"
137 #include "gdkwin32.h"
138 #include "gdkwin32dnd.h"
139 #include "gdkdisplayprivate.h"
140 #include "gdk/gdkdndprivate.h"
141 #include "gdkwin32dnd-private.h"
142 #include "gdkdisplay-win32.h"
143 #include "gdkselection-win32.h"
144
145 #include <ole2.h>
146
147 #include <shlobj.h>
148 #include <shlguid.h>
149 #include <objidl.h>
150
151 #include <gdk/gdk.h>
152 #include <glib/gstdio.h>
153
154 /* from gdkselection-win32.c */
155 extern GdkAtom *known_pixbuf_formats;
156 extern int n_known_pixbuf_formats;
157
158
159 typedef enum {
160 GDK_DRAG_STATUS_DRAG,
161 GDK_DRAG_STATUS_MOTION_WAIT,
162 GDK_DRAG_STATUS_ACTION_WAIT,
163 GDK_DRAG_STATUS_DROP
164 } GdkDragStatus;
165
166 static GList *contexts;
167 static GdkDragContext *current_dest_drag = NULL;
168 static gboolean use_ole2_dnd = FALSE;
169
170 G_DEFINE_TYPE (GdkWin32DragContext, gdk_win32_drag_context, GDK_TYPE_DRAG_CONTEXT)
171
172 static void
173 move_drag_window (GdkDragContext *context,
174 guint x_root,
175 guint y_root)
176 {
177 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
178
179 gdk_window_move (context_win32->drag_window,
180 x_root - context_win32->hot_x,
181 y_root - context_win32->hot_y);
182 gdk_window_raise (context_win32->drag_window);
183 }
184
185 static void
186 gdk_win32_drag_context_init (GdkWin32DragContext *context)
187 {
188 if (!use_ole2_dnd)
189 {
190 contexts = g_list_prepend (contexts, context);
191 }
192 else
193 {
194 }
195
196 GDK_NOTE (DND, g_print ("gdk_drag_context_init %p\n", context));
197 }
198
199 static void
200 gdk_win32_drag_context_finalize (GObject *object)
201 {
202 GdkDragContext *context;
203 GdkWin32DragContext *context_win32;
204 GdkWindow *drag_window;
205
206 GDK_NOTE (DND, g_print ("gdk_drag_context_finalize %p\n", object));
207
208 g_return_if_fail (GDK_IS_WIN32_DRAG_CONTEXT (object));
209
210 context = GDK_DRAG_CONTEXT (object);
211 context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
212
213 if (!use_ole2_dnd)
214 {
215 contexts = g_list_remove (contexts, context);
216
217 if (context == current_dest_drag)
218 current_dest_drag = NULL;
219 }
220
221 drag_window = context_win32->drag_window;
222
223 g_array_unref (context_win32->droptarget_format_target_map);
224
225 G_OBJECT_CLASS (gdk_win32_drag_context_parent_class)->finalize (object);
226
227 if (drag_window)
228 gdk_window_destroy (drag_window);
229 }
230
231 /* Drag Contexts */
232
233 static GdkDragContext *
234 gdk_drag_context_new (GdkDisplay *display)
235 {
236 GdkWin32DragContext *context_win32;
237 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (display);
238 GdkDragContext *context;
239
240 context_win32 = g_object_new (GDK_TYPE_WIN32_DRAG_CONTEXT, NULL);
241 context = GDK_DRAG_CONTEXT(context_win32);
242 context->display = display;
243
244 gdk_drag_context_set_device (context, gdk_seat_get_pointer (gdk_display_get_default_seat (display)));
245
246 if (win32_display->has_fixed_scale)
247 context_win32->scale = win32_display->window_scale;
248 else
249 context_win32->scale = _gdk_win32_display_get_monitor_scale_factor (win32_display, NULL, NULL, NULL);
250
251 context_win32->droptarget_format_target_map = g_array_new (FALSE, FALSE, sizeof (GdkSelTargetFormat));
252
253 return context;
254 }
255
256 static GdkDragContext *
257 gdk_drag_context_find (gboolean is_source,
258 GdkWindow *source,
259 GdkWindow *dest)
260 {
261 GList *tmp_list = contexts;
262 GdkDragContext *context;
263
264 while (tmp_list)
265 {
266 context = (GdkDragContext *)tmp_list->data;
267
268 if ((!context->is_source == !is_source) &&
269 ((source == NULL) || (context->source_window && (context->source_window == source))) &&
270 ((dest == NULL) || (context->dest_window && (context->dest_window == dest))))
271 return context;
272
273 tmp_list = tmp_list->next;
274 }
275
276 return NULL;
277 }
278
279 #define PRINT_GUID(guid) \
280 g_print ("%.08lx-%.04x-%.04x-%.02x%.02x-%.02x%.02x%.02x%.02x%.02x%.02x", \
281 ((gulong *) guid)[0], \
282 ((gushort *) guid)[2], \
283 ((gushort *) guid)[3], \
284 ((guchar *) guid)[8], \
285 ((guchar *) guid)[9], \
286 ((guchar *) guid)[10], \
287 ((guchar *) guid)[11], \
288 ((guchar *) guid)[12], \
289 ((guchar *) guid)[13], \
290 ((guchar *) guid)[14], \
291 ((guchar *) guid)[15]);
292
293 typedef struct {
294 IDropTarget idt;
295 GdkDragContext *context;
296
297 gint ref_count;
298 GdkWindow *dest_window;
299
300 } target_drag_context;
301
302 typedef struct {
303 IDropSource ids;
304 GdkDragContext *context;
305 gint ref_count;
306 } source_drag_context;
307
308 typedef struct {
309 IDataObject ido;
310 int ref_count;
311 GdkDragContext *context;
312 GArray *formats;
313 } data_object;
314
315 typedef struct {
316 IEnumFORMATETC ief;
317 int ref_count;
318 int ix;
319 data_object *dataobj;
320 } enum_formats;
321
322 static source_drag_context *pending_src_context = NULL;
323 static source_drag_context *current_src_context = NULL;
324 static data_object *current_src_object = NULL;
325
326 static enum_formats *enum_formats_new (data_object *dataobj);
327
328 /* map windows -> target drag contexts. The table
329 * owns a ref to both objects.
330 */
331 static GHashTable* target_ctx_for_window = NULL;
332
333 static ULONG STDMETHODCALLTYPE
334 idroptarget_addref (LPDROPTARGET This)
335 {
336 target_drag_context *ctx = (target_drag_context *) This;
337
338 int ref_count = ++ctx->ref_count;
339
340 GDK_NOTE (DND, g_print ("idroptarget_addref %p %d\n", This, ref_count));
341
342 return ref_count;
343 }
344
345 static HRESULT STDMETHODCALLTYPE
346 idroptarget_queryinterface (LPDROPTARGET This,
347 REFIID riid,
348 LPVOID *ppvObject)
349 {
350 GDK_NOTE (DND, {
351 g_print ("idroptarget_queryinterface %p ", This);
352 PRINT_GUID (riid);
353 });
354
355 *ppvObject = NULL;
356
357 if (IsEqualGUID (riid, &IID_IUnknown))
358 {
359 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
360 idroptarget_addref (This);
361 *ppvObject = This;
362 return S_OK;
363 }
364 else if (IsEqualGUID (riid, &IID_IDropTarget))
365 {
366 GDK_NOTE (DND, g_print ("...IDropTarget S_OK\n"));
367 idroptarget_addref (This);
368 *ppvObject = This;
369 return S_OK;
370 }
371 else
372 {
373 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
374 return E_NOINTERFACE;
375 }
376 }
377
378 static ULONG STDMETHODCALLTYPE
379 idroptarget_release (LPDROPTARGET This)
380 {
381 target_drag_context *ctx = (target_drag_context *) This;
382
383 int ref_count = --ctx->ref_count;
384
385 GDK_NOTE (DND, g_print ("idroptarget_release %p %d\n", This, ref_count));
386
387 if (ref_count == 0)
388 {
389 g_object_unref (ctx->context);
390 g_clear_object (&ctx->dest_window);
391 g_free (This);
392 }
393
394 return ref_count;
395 }
396
397 static GdkDragAction
398 get_suggested_action (DWORD grfKeyState)
399 {
400 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
401 /* This is the yucky Windows standard: Force link action if both
402 * Control and Alt are down, copy if Control is down alone, move if
403 * Alt is down alone, or use default of move within the app or copy
404 * when origin of the drag is in another app.
405 */
406 if (grfKeyState & MK_CONTROL && grfKeyState & MK_SHIFT)
407 return GDK_ACTION_LINK; /* Link action not supported */
408 else if (grfKeyState & MK_CONTROL)
409 return GDK_ACTION_COPY;
410 else if (grfKeyState & MK_ALT)
411 return GDK_ACTION_MOVE;
412 else if (sel_win32->dnd_source_state == GDK_WIN32_DND_DRAGGING)
413 return GDK_ACTION_MOVE;
414 else
415 return GDK_ACTION_COPY;
416 /* Any way to determine when to add in DROPEFFECT_SCROLL? */
417 }
418
419 /* Process pending events -- we don't want to service non-GUI events
420 * forever so do one iteration and then do more only if there’s a
421 * pending GDK event.
422 */
423 static void
424 process_pending_events (GdkDisplay *display)
425 {
426 g_main_context_iteration (NULL, FALSE);
427 while (_gdk_event_queue_find_first (display))
428 g_main_context_iteration (NULL, FALSE);
429 }
430
431 static DWORD
432 drop_effect_for_action (GdkDragAction action)
433 {
434 switch (action)
435 {
436 case GDK_ACTION_MOVE:
437 return DROPEFFECT_MOVE;
438 case GDK_ACTION_LINK:
439 return DROPEFFECT_LINK;
440 case GDK_ACTION_COPY:
441 return DROPEFFECT_COPY;
442 default:
443 return DROPEFFECT_NONE;
444 }
445 }
446
447 static GdkDragAction
448 action_for_drop_effect (DWORD effect)
449 {
450 switch (effect)
451 {
452 case DROPEFFECT_MOVE:
453 return GDK_ACTION_MOVE;
454 case DROPEFFECT_LINK:
455 return GDK_ACTION_LINK;
456 case DROPEFFECT_COPY:
457 return GDK_ACTION_COPY;
458 default:
459 return 0;
460 }
461 }
462
463 static void
464 dnd_event_put (GdkEventType type,
465 GdkDragContext *context,
466 gint pt_x,
467 gint pt_y,
468 gboolean to_dest_window)
469 {
470 GdkEvent *e;
471
472 e = gdk_event_new (type);
473
474 if (to_dest_window)
475 g_set_object (&e->dnd.window, context->dest_window);
476 else
477 g_set_object (&e->dnd.window, context->source_window);
478 e->dnd.send_event = FALSE;
479 g_set_object (&e->dnd.context, context);
480 e->dnd.time = GDK_CURRENT_TIME;
481 e->dnd.x_root = pt_x;
482 e->dnd.y_root = pt_y;
483
484 gdk_event_set_device (e, gdk_drag_context_get_device (context));
485 gdk_event_set_seat (e, gdk_device_get_seat (gdk_drag_context_get_device (context)));
486
487 GDK_NOTE (EVENTS, _gdk_win32_print_event (e));
488 gdk_event_put (e);
489 gdk_event_free (e);
490 }
491
492 static GList *
493 query_targets (LPDATAOBJECT pDataObj,
494 GArray *format_target_map)
495 {
496 IEnumFORMATETC *pfmt = NULL;
497 FORMATETC fmt;
498 GList *result = NULL;
499 HRESULT hr;
500
501 if ((LPDATAOBJECT) current_src_object == pDataObj)
502 return g_list_copy (current_src_object->context->targets);
503
504 hr = IDataObject_EnumFormatEtc (pDataObj, DATADIR_GET, &pfmt);
505
506 if (SUCCEEDED (hr))
507 hr = IEnumFORMATETC_Next (pfmt, 1, &fmt, NULL);
508
509 while (SUCCEEDED (hr) && hr != S_FALSE)
510 {
511 gboolean is_predef;
512 gchar *registered_name = _gdk_win32_get_clipboard_format_name (fmt.cfFormat, &is_predef);
513
514 if (registered_name && is_predef)
515 GDK_NOTE (DND, g_print ("supported built-in source format 0x%x %s\n", fmt.cfFormat, registered_name));
516 else if (registered_name)
517 GDK_NOTE (DND, g_print ("supported source format 0x%x %s\n", fmt.cfFormat, registered_name));
518 else
519 GDK_NOTE (DND, g_print ("supported unnamed? source format 0x%x\n", fmt.cfFormat));
520
521 g_free (registered_name);
522
523 _gdk_win32_add_format_to_targets (fmt.cfFormat, format_target_map, &result);
524 hr = IEnumFORMATETC_Next (pfmt, 1, &fmt, NULL);
525 }
526
527 if (pfmt)
528 IEnumFORMATETC_Release (pfmt);
529
530 return g_list_reverse (result);
531 }
532
533 static void
534 set_data_object (LPDATAOBJECT *location, LPDATAOBJECT data_object)
535 {
536 if (*location != NULL)
537 IDataObject_Release (*location);
538
539 *location = data_object;
540
541 if (*location != NULL)
542 IDataObject_AddRef (*location);
543 }
544
545 static HRESULT STDMETHODCALLTYPE
546 idroptarget_dragenter (LPDROPTARGET This,
547 LPDATAOBJECT pDataObj,
548 DWORD grfKeyState,
549 POINTL pt,
550 LPDWORD pdwEffect)
551 {
552 target_drag_context *ctx = (target_drag_context *) This;
553 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
554 GdkDragContext *context;
555 GdkWin32DragContext *context_win32;
556 gint pt_x;
557 gint pt_y;
558
559 GDK_NOTE (DND, g_print ("idroptarget_dragenter %p @ %ld : %ld for dest window 0x%p S_OK\n", This, pt.x, pt.y, ctx->dest_window));
560
561 g_clear_object (&ctx->context);
562
563 context = gdk_drag_context_new (gdk_window_get_display (ctx->dest_window));
564 context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
565 ctx->context = context;
566 g_set_object (&context->dest_window, ctx->dest_window);
567
568 context->protocol = GDK_DRAG_PROTO_OLE2;
569 context->is_source = FALSE;
570 context->source_window = NULL;
571
572 /* OLE2 DnD does not allow us to get the source window,
573 * but we *can* find it if it's ours. This is needed to
574 * support DnD within the same widget, for example.
575 */
576 if (current_src_context && current_src_context->context)
577 g_set_object (&context->source_window, current_src_context->context->source_window);
578 else
579 g_set_object (&context->source_window, gdk_get_default_root_window ());
580
581 g_set_object (&sel_win32->target_drag_context, context);
582 context->actions = GDK_ACTION_DEFAULT | GDK_ACTION_COPY | GDK_ACTION_MOVE;
583 context->suggested_action = GDK_ACTION_MOVE;
584 context->action = GDK_ACTION_MOVE;
585
586 g_array_set_size (context_win32->droptarget_format_target_map, 0);
587 context->targets = query_targets (pDataObj, context_win32->droptarget_format_target_map);
588
589 ctx->context->suggested_action = get_suggested_action (grfKeyState);
590 set_data_object (&sel_win32->dnd_data_object_target, pDataObj);
591 pt_x = (pt.x + _gdk_offset_x) / context_win32->scale;
592 pt_y = (pt.y + _gdk_offset_y) / context_win32->scale;
593 dnd_event_put (GDK_DRAG_ENTER, ctx->context, pt_x, pt_y, TRUE);
594 dnd_event_put (GDK_DRAG_MOTION, ctx->context, pt_x, pt_y, TRUE);
595 context_win32->last_key_state = grfKeyState;
596 context_win32->last_x = pt_x;
597 context_win32->last_y = pt_y;
598 process_pending_events (gdk_device_get_display (gdk_drag_context_get_device (ctx->context)));
599 *pdwEffect = drop_effect_for_action (ctx->context->action);
600
601 GDK_NOTE (DND, g_print ("idroptarget_dragenter returns with action %d and drop effect %lu\n", ctx->context->action, *pdwEffect));
602
603 return S_OK;
604 }
605
606 static HRESULT STDMETHODCALLTYPE
607 idroptarget_dragover (LPDROPTARGET This,
608 DWORD grfKeyState,
609 POINTL pt,
610 LPDWORD pdwEffect)
611 {
612 target_drag_context *ctx = (target_drag_context *) This;
613 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (ctx->context);
614 gint pt_x = (pt.x + _gdk_offset_x) / context_win32->scale;
615 gint pt_y = (pt.y + _gdk_offset_y) / context_win32->scale;
616
617 ctx->context->suggested_action = get_suggested_action (grfKeyState);
618
619 GDK_NOTE (DND, g_print ("idroptarget_dragover %p @ %ld : %ld, suggests %d action S_OK\n", This, pt.x, pt.y, ctx->context->suggested_action));
620
621 if (pt_x != context_win32->last_x ||
622 pt_y != context_win32->last_y ||
623 grfKeyState != context_win32->last_key_state)
624 {
625 dnd_event_put (GDK_DRAG_MOTION, ctx->context, pt_x, pt_y, TRUE);
626 context_win32->last_key_state = grfKeyState;
627 context_win32->last_x = pt_x;
628 context_win32->last_y = pt_y;
629 }
630
631 process_pending_events (gdk_device_get_display (gdk_drag_context_get_device (ctx->context)));
632
633 *pdwEffect = drop_effect_for_action (ctx->context->action);
634
635 GDK_NOTE (DND, g_print ("idroptarget_dragover returns with action %d and effect %lu\n", ctx->context->action, *pdwEffect));
636
637 return S_OK;
638 }
639
640 static HRESULT STDMETHODCALLTYPE
641 idroptarget_dragleave (LPDROPTARGET This)
642 {
643 target_drag_context *ctx = (target_drag_context *) This;
644 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
645
646 GDK_NOTE (DND, g_print ("idroptarget_dragleave %p S_OK\n", This));
647
648 dnd_event_put (GDK_DRAG_LEAVE, ctx->context, 0, 0, TRUE);
649 process_pending_events (gdk_device_get_display (gdk_drag_context_get_device (ctx->context)));
650
651 g_clear_object (&sel_win32->target_drag_context);
652 g_clear_object (&ctx->context);
653 set_data_object (&sel_win32->dnd_data_object_target, NULL);
654
655 return S_OK;
656 }
657
658 static HRESULT STDMETHODCALLTYPE
659 idroptarget_drop (LPDROPTARGET This,
660 LPDATAOBJECT pDataObj,
661 DWORD grfKeyState,
662 POINTL pt,
663 LPDWORD pdwEffect)
664 {
665 target_drag_context *ctx = (target_drag_context *) This;
666 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (ctx->context);
667 gint pt_x = (pt.x + _gdk_offset_x) / context_win32->scale;
668 gint pt_y = (pt.y + _gdk_offset_y) / context_win32->scale;
669 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
670
671 GDK_NOTE (DND, g_print ("idroptarget_drop %p ", This));
672
673 if (pDataObj == NULL)
674 {
675 GDK_NOTE (DND, g_print ("E_POINTER\n"));
676 g_clear_object (&ctx->context);
677 return E_POINTER;
678 }
679
680 ctx->context->suggested_action = get_suggested_action (grfKeyState);
681
682 dnd_event_put (GDK_DROP_START, ctx->context, pt_x, pt_y, TRUE);
683 process_pending_events (gdk_device_get_display (gdk_drag_context_get_device (ctx->context)));
684
685 /* Notify OLE of copy or move */
686 if (sel_win32->dnd_target_state != GDK_WIN32_DND_DROPPED)
687 *pdwEffect = DROPEFFECT_NONE;
688 else
689 *pdwEffect = drop_effect_for_action (ctx->context->action);
690
691 g_clear_object (&sel_win32->target_drag_context);
692 g_clear_object (&ctx->context);
693 set_data_object (&sel_win32->dnd_data_object_target, NULL);
694
695 GDK_NOTE (DND, g_print ("drop S_OK with effect %lx\n", *pdwEffect));
696
697 return S_OK;
698 }
699
700 static ULONG STDMETHODCALLTYPE
701 idropsource_addref (LPDROPSOURCE This)
702 {
703 source_drag_context *ctx = (source_drag_context *) This;
704
705 int ref_count = ++ctx->ref_count;
706
707 GDK_NOTE (DND, g_print ("idropsource_addref %p %d\n", This, ref_count));
708
709 return ref_count;
710 }
711
712 static HRESULT STDMETHODCALLTYPE
713 idropsource_queryinterface (LPDROPSOURCE This,
714 REFIID riid,
715 LPVOID *ppvObject)
716 {
717 GDK_NOTE (DND, {
718 g_print ("idropsource_queryinterface %p ", This);
719 PRINT_GUID (riid);
720 });
721
722 *ppvObject = NULL;
723
724 if (IsEqualGUID (riid, &IID_IUnknown))
725 {
726 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
727 idropsource_addref (This);
728 *ppvObject = This;
729 return S_OK;
730 }
731 else if (IsEqualGUID (riid, &IID_IDropSource))
732 {
733 GDK_NOTE (DND, g_print ("...IDropSource S_OK\n"));
734 idropsource_addref (This);
735 *ppvObject = This;
736 return S_OK;
737 }
738 else
739 {
740 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
741 return E_NOINTERFACE;
742 }
743 }
744
745 static ULONG STDMETHODCALLTYPE
746 idropsource_release (LPDROPSOURCE This)
747 {
748 source_drag_context *ctx = (source_drag_context *) This;
749
750 int ref_count = --ctx->ref_count;
751
752 GDK_NOTE (DND, g_print ("idropsource_release %p %d\n", This, ref_count));
753
754 if (ref_count == 0)
755 {
756 g_clear_object (&ctx->context);
757 if (current_src_context == ctx)
758 current_src_context = NULL;
759 g_free (This);
760 }
761
762 return ref_count;
763 }
764
765 /* Emit GDK events for any changes in mouse events or control key
766 * state since the last recorded state. Return true if any events
767 * have been emitted and false otherwise.
768 */
769 static gboolean
770 send_change_events (GdkDragContext *context,
771 DWORD key_state,
772 gboolean esc_pressed)
773 {
774 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
775 POINT pt;
776 POINT pt_client;
777 HMONITOR monitor = NULL;
778 gboolean changed = FALSE;
779 HWND hwnd = GDK_WINDOW_HWND (context->source_window);
780 LPARAM lparam;
781 WPARAM wparam;
782 gint pt_x;
783 gint pt_y;
784
785 if (!API_CALL (GetCursorPos, (&pt)))
786 return FALSE;
787
788 /* Move the DND IPC window to the monitor the cursor is currently on.
789 This esures that OLE2 DND works correctly even if the DPI awareness
790 isn't per-monitor.
791 */
792 monitor = MonitorFromPoint (pt, MONITOR_DEFAULTTONEAREST);
793 if (monitor != context_win32->last_monitor)
794 {
795 MONITORINFO mi;
796
797 mi.cbSize = sizeof(mi);
798 if (GetMonitorInfoW (monitor, &mi))
799 {
800 MoveWindow (hwnd, mi.rcWork.left, mi.rcWork.top, 1, 1, FALSE);
801 }
802
803 context_win32->last_monitor = monitor;
804 }
805
806 pt_client = pt;
807
808 if (!API_CALL (ScreenToClient, (hwnd, &pt_client)))
809 return FALSE;
810
811 pt_x = (pt.x + _gdk_offset_x) / context_win32->scale;
812 pt_y = (pt.y + _gdk_offset_y) / context_win32->scale;
813
814 if (pt_x != context_win32->last_x || pt_y != context_win32->last_y ||
815 key_state != context_win32->last_key_state)
816 {
817 lparam = MAKELPARAM (pt_client.x, pt_client.y);
818 wparam = key_state;
819 if (pt_x != context_win32->last_x || pt_y != context_win32->last_y)
820 {
821 GDK_NOTE (DND, g_print ("Sending WM_MOUSEMOVE (%ld,%ld)\n", pt.x, pt.y));
822 SendMessage (hwnd, WM_MOUSEMOVE, wparam, lparam);
823 }
824
825 if ((key_state & MK_LBUTTON) != (context_win32->last_key_state & MK_LBUTTON))
826 {
827 if (key_state & MK_LBUTTON)
828 SendMessage (hwnd, WM_LBUTTONDOWN, wparam, lparam);
829 else
830 SendMessage (hwnd, WM_LBUTTONUP, wparam, lparam);
831 }
832 if ((key_state & MK_MBUTTON) != (context_win32->last_key_state & MK_MBUTTON))
833 {
834 if (key_state & MK_MBUTTON)
835 SendMessage (hwnd, WM_MBUTTONDOWN, wparam, lparam);
836 else
837 SendMessage (hwnd, WM_MBUTTONUP, wparam, lparam);
838 }
839 if ((key_state & MK_RBUTTON) != (context_win32->last_key_state & MK_RBUTTON))
840 {
841 if (key_state & MK_RBUTTON)
842 SendMessage (hwnd, WM_RBUTTONDOWN, wparam, lparam);
843 else
844 SendMessage (hwnd, WM_RBUTTONUP, wparam, lparam);
845 }
846 if ((key_state & MK_CONTROL) != (context_win32->last_key_state & MK_CONTROL))
847 {
848 if (key_state & MK_CONTROL)
849 SendMessage (hwnd, WM_KEYDOWN, VK_CONTROL, 0);
850 else
851 SendMessage (hwnd, WM_KEYUP, VK_CONTROL, 0);
852 }
853 if ((key_state & MK_SHIFT) != (context_win32->last_key_state & MK_SHIFT))
854 {
855 if (key_state & MK_SHIFT)
856 SendMessage (hwnd, WM_KEYDOWN, VK_SHIFT, 0);
857 else
858 SendMessage (hwnd, WM_KEYUP, VK_SHIFT, 0);
859 }
860
861 changed = TRUE;
862 context_win32->last_key_state = key_state;
863 context_win32->last_x = pt_x;
864 context_win32->last_y = pt_y;
865 }
866
867 if (esc_pressed)
868 {
869 GDK_NOTE (DND, g_print ("Sending a escape key down message to %p\n", hwnd));
870 SendMessage (hwnd, WM_KEYDOWN, VK_ESCAPE, 0);
871 changed = TRUE;
872 }
873
874 return changed;
875 }
876
877 static HRESULT STDMETHODCALLTYPE
878 idropsource_querycontinuedrag (LPDROPSOURCE This,
879 BOOL fEscapePressed,
880 DWORD grfKeyState)
881 {
882 source_drag_context *ctx = (source_drag_context *) This;
883 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
884
885 GDK_NOTE (DND, g_print ("idropsource_querycontinuedrag %p esc=%d keystate=0x%lx ", This, fEscapePressed, grfKeyState));
886
887 if (send_change_events (ctx->context, grfKeyState, fEscapePressed))
888 process_pending_events (gdk_device_get_display (gdk_drag_context_get_device (ctx->context)));
889
890 if (sel_win32->dnd_source_state == GDK_WIN32_DND_DROPPED)
891 {
892 GDK_NOTE (DND, g_print ("DRAGDROP_S_DROP\n"));
893 return DRAGDROP_S_DROP;
894 }
895 else if (sel_win32->dnd_source_state == GDK_WIN32_DND_NONE)
896 {
897 GDK_NOTE (DND, g_print ("DRAGDROP_S_CANCEL\n"));
898 return DRAGDROP_S_CANCEL;
899 }
900 else
901 {
902 GDK_NOTE (DND, g_print ("S_OK\n"));
903 return S_OK;
904 }
905 }
906
907 static HRESULT STDMETHODCALLTYPE
908 idropsource_givefeedback (LPDROPSOURCE This,
909 DWORD dwEffect)
910 {
911 source_drag_context *ctx = (source_drag_context *) This;
912 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (ctx->context);
913 GdkDragAction suggested_action;
914 GdkEvent *e;
915 POINT pt;
916
917 GDK_NOTE (DND, g_print ("idropsource_givefeedback %p with drop effect %lu S_OK\n", This, dwEffect));
918
919 if (!API_CALL (GetCursorPos, (&pt)))
920 return S_OK;
921
922 suggested_action = action_for_drop_effect (dwEffect);
923
924 ctx->context->action = suggested_action;
925
926 if (dwEffect == DROPEFFECT_NONE)
927 g_clear_object (&ctx->context->dest_window);
928 else if (ctx->context->dest_window == NULL)
929 ctx->context->dest_window = g_object_ref (gdk_get_default_root_window ());
930
931 context_win32->last_x = (pt.x + _gdk_offset_x) / context_win32->scale;
932 context_win32->last_y = (pt.y + _gdk_offset_y) / context_win32->scale;
933
934 e = gdk_event_new (GDK_DRAG_STATUS);
935
936 g_set_object (&e->dnd.window, ctx->context->source_window);
937 e->dnd.send_event = FALSE;
938 g_set_object (&e->dnd.context, ctx->context);
939 e->dnd.time = GDK_CURRENT_TIME;
940 e->dnd.x_root = context_win32->last_x;
941 e->dnd.y_root = context_win32->last_y;
942
943 gdk_event_set_device (e, gdk_drag_context_get_device (ctx->context));
944 gdk_event_set_seat (e, gdk_device_get_seat (gdk_drag_context_get_device (ctx->context)));
945
946 GDK_NOTE (EVENTS, _gdk_win32_print_event (e));
947 gdk_event_put (e);
948 gdk_event_free (e);
949 process_pending_events (gdk_device_get_display (gdk_drag_context_get_device (ctx->context)));
950
951 GDK_NOTE (DND, g_print ("idropsource_givefeedback %p returns\n", This));
952
953 return S_OK;
954 }
955
956 static ULONG STDMETHODCALLTYPE
957 idataobject_addref (LPDATAOBJECT This)
958 {
959 data_object *dobj = (data_object *) This;
960 int ref_count = ++dobj->ref_count;
961
962 GDK_NOTE (DND, g_print ("idataobject_addref %p %d\n", This, ref_count));
963
964 return ref_count;
965 }
966
967 static HRESULT STDMETHODCALLTYPE
968 idataobject_queryinterface (LPDATAOBJECT This,
969 REFIID riid,
970 LPVOID *ppvObject)
971 {
972 GDK_NOTE (DND, {
973 g_print ("idataobject_queryinterface %p ", This);
974 PRINT_GUID (riid);
975 });
976
977 *ppvObject = NULL;
978
979 if (IsEqualGUID (riid, &IID_IUnknown))
980 {
981 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
982 idataobject_addref (This);
983 *ppvObject = This;
984 return S_OK;
985 }
986 else if (IsEqualGUID (riid, &IID_IDataObject))
987 {
988 GDK_NOTE (DND, g_print ("...IDataObject S_OK\n"));
989 idataobject_addref (This);
990 *ppvObject = This;
991 return S_OK;
992 }
993 else
994 {
995 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
996 return E_NOINTERFACE;
997 }
998 }
999
1000 static ULONG STDMETHODCALLTYPE
1001 idataobject_release (LPDATAOBJECT This)
1002 {
1003 data_object *dobj = (data_object *) This;
1004 int ref_count = --dobj->ref_count;
1005
1006 GDK_NOTE (DND, g_print ("idataobject_release %p %d\n", This, ref_count));
1007
1008 if (ref_count == 0)
1009 {
1010 g_array_free (dobj->formats, TRUE);
1011 g_free (This);
1012 }
1013
1014 return ref_count;
1015 }
1016
1017 static HRESULT
1018 query (LPDATAOBJECT This,
1019 LPFORMATETC pFormatEtc)
1020 {
1021 data_object *ctx = (data_object *) This;
1022 gint i;
1023
1024 if (!pFormatEtc)
1025 return DV_E_FORMATETC;
1026
1027 if (pFormatEtc->lindex != -1)
1028 return DV_E_LINDEX;
1029
1030 if ((pFormatEtc->tymed & TYMED_HGLOBAL) == 0)
1031 return DV_E_TYMED;
1032
1033 if ((pFormatEtc->dwAspect & DVASPECT_CONTENT) == 0)
1034 return DV_E_DVASPECT;
1035
1036 for (i = 0; i < ctx->formats->len; i++)
1037 if (pFormatEtc->cfFormat == g_array_index (ctx->formats, GdkSelTargetFormat, i).format)
1038 return S_OK;
1039
1040 return DV_E_FORMATETC;
1041 }
1042
1043 static HRESULT STDMETHODCALLTYPE
1044 idataobject_getdata (LPDATAOBJECT This,
1045 LPFORMATETC pFormatEtc,
1046 LPSTGMEDIUM pMedium)
1047 {
1048 data_object *ctx = (data_object *) This;
1049 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
1050 HRESULT hr;
1051 GdkEvent e;
1052 gint i;
1053 GdkAtom target;
1054 gint64 loopend;
1055
1056 GDK_NOTE (DND, g_print ("idataobject_getdata %p %s ",
1057 This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
1058
1059 /* Check whether we can provide requested format */
1060 hr = query (This, pFormatEtc);
1061 if (hr != S_OK)
1062 {
1063 GDK_NOTE (DND, g_print ("Unsupported format, returning 0x%lx\n", hr));
1064 return hr;
1065 }
1066
1067 /* Append a GDK_SELECTION_REQUEST event and then hope the app sets the
1068 * property associated with the _gdk_ole2_dnd atom
1069 */
1070 win32_sel->property_change_format = pFormatEtc->cfFormat;
1071 win32_sel->property_change_data = pMedium;
1072
1073 for (i = 0, target = NULL; i < ctx->formats->len; i++)
1074 {
1075 GdkSelTargetFormat *frec = &g_array_index (ctx->formats, GdkSelTargetFormat, i);
1076
1077 if (frec->format == pFormatEtc->cfFormat)
1078 {
1079 target = frec->target;
1080 win32_sel->property_change_transmute = frec->transmute;
1081 win32_sel->property_change_target_atom = frec->target;
1082 }
1083 }
1084
1085 if (target == NULL)
1086 {
1087 GDK_NOTE (EVENTS, g_print ("(target not found)"));
1088 return E_UNEXPECTED;
1089 }
1090
1091 GDK_NOTE (DND, {
1092 gchar *target_name = gdk_atom_name (target);
1093 g_print ("idataobject_getdata will request target 0x%p (%s) ",
1094 target, target_name);
1095 g_free (target_name);
1096 });
1097
1098
1099 memset (&e, 0, sizeof (GdkEvent));
1100 e.type = GDK_SELECTION_REQUEST;
1101 g_set_object (&e.selection.window, ctx->context->source_window);
1102 e.selection.send_event = FALSE; /* ??? */
1103 /* Both selection and property are OLE2_DND, because change_property()
1104 * will only get the property and not the selection. Theoretically we
1105 * could use two different atoms (SELECTION_OLE2_DND and PROPERTY_OLE2_DND),
1106 * but there's little reason to do so.
1107 */
1108 e.selection.selection = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND);
1109 e.selection.target = target;
1110 /* Requestor here is fake, just to allow the event to be processed */
1111 g_set_object (&e.selection.requestor, ctx->context->source_window);
1112 e.selection.property = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND);
1113 e.selection.time = GDK_CURRENT_TIME;
1114
1115 GDK_NOTE (EVENTS, _gdk_win32_print_event (&e));
1116
1117 gdk_event_put (&e);
1118
1119 /* Don't hold up longer than one second */
1120 loopend = g_get_monotonic_time () + 1000000000;
1121
1122 while (win32_sel->property_change_data != 0 &&
1123 g_get_monotonic_time () < loopend)
1124 process_pending_events (gdk_device_get_display (gdk_drag_context_get_device (ctx->context)));
1125
1126 if (pMedium->hGlobal == NULL) {
1127 GDK_NOTE (DND, g_print (" E_UNEXPECTED\n"));
1128 return E_UNEXPECTED;
1129 }
1130
1131 GDK_NOTE (DND, g_print (" S_OK\n"));
1132 return S_OK;
1133 }
1134
1135 static HRESULT STDMETHODCALLTYPE
1136 idataobject_getdatahere (LPDATAOBJECT This,
1137 LPFORMATETC pFormatEtc,
1138 LPSTGMEDIUM pMedium)
1139 {
1140 GDK_NOTE (DND, g_print ("idataobject_getdatahere %p %s E_UNEXPECTED\n",
1141 This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
1142
1143 return E_UNEXPECTED;
1144 }
1145
1146 static HRESULT STDMETHODCALLTYPE
1147 idataobject_querygetdata (LPDATAOBJECT This,
1148 LPFORMATETC pFormatEtc)
1149 {
1150 HRESULT hr;
1151
1152 hr = query (This, pFormatEtc);
1153
1154 #define CASE(x) case x: g_print (#x "\n"); break
1155 GDK_NOTE (DND, {
1156 g_print ("idataobject_querygetdata %p %s ",
1157 This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat));
1158 switch (hr)
1159 {
1160 CASE (DV_E_FORMATETC);
1161 CASE (DV_E_LINDEX);
1162 CASE (DV_E_TYMED);
1163 CASE (DV_E_DVASPECT);
1164 CASE (S_OK);
1165 default: g_print ("%#lx", hr);
1166 }
1167 });
1168
1169 return hr;
1170 }
1171
1172 static HRESULT STDMETHODCALLTYPE
1173 idataobject_getcanonicalformatetc (LPDATAOBJECT This,
1174 LPFORMATETC pFormatEtcIn,
1175 LPFORMATETC pFormatEtcOut)
1176 {
1177 GDK_NOTE (DND, g_print ("idataobject_getcanonicalformatetc %p E_UNEXPECTED\n", This));
1178
1179 return E_UNEXPECTED;
1180 }
1181
1182 static HRESULT STDMETHODCALLTYPE
1183 idataobject_setdata (LPDATAOBJECT This,
1184 LPFORMATETC pFormatEtc,
1185 LPSTGMEDIUM pMedium,
1186 BOOL fRelease)
1187 {
1188 GDK_NOTE (DND, g_print ("idataobject_setdata %p %s E_UNEXPECTED\n",
1189 This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
1190
1191 return E_UNEXPECTED;
1192 }
1193
1194 static HRESULT STDMETHODCALLTYPE
1195 idataobject_enumformatetc (LPDATAOBJECT This,
1196 DWORD dwDirection,
1197 LPENUMFORMATETC *ppEnumFormatEtc)
1198 {
1199 GDK_NOTE (DND, g_print ("idataobject_enumformatetc %p ", This));
1200
1201 if (dwDirection != DATADIR_GET)
1202 {
1203 GDK_NOTE (DND, g_print ("E_NOTIMPL\n"));
1204 return E_NOTIMPL;
1205 }
1206
1207 *ppEnumFormatEtc = &enum_formats_new ((data_object *) This)->ief;
1208
1209 GDK_NOTE (DND, g_print (" %p S_OK\n", *ppEnumFormatEtc));
1210
1211 return S_OK;
1212 }
1213
1214 static HRESULT STDMETHODCALLTYPE
1215 idataobject_dadvise (LPDATAOBJECT This,
1216 LPFORMATETC pFormatetc,
1217 DWORD advf,
1218 LPADVISESINK pAdvSink,
1219 DWORD *pdwConnection)
1220 {
1221 GDK_NOTE (DND, g_print ("idataobject_dadvise %p E_NOTIMPL\n", This));
1222
1223 return E_NOTIMPL;
1224 }
1225
1226 static HRESULT STDMETHODCALLTYPE
1227 idataobject_dunadvise (LPDATAOBJECT This,
1228 DWORD dwConnection)
1229 {
1230 GDK_NOTE (DND, g_print ("idataobject_dunadvise %p E_NOTIMPL\n", This));
1231
1232 return E_NOTIMPL;
1233 }
1234
1235 static HRESULT STDMETHODCALLTYPE
1236 idataobject_enumdadvise (LPDATAOBJECT This,
1237 LPENUMSTATDATA *ppenumAdvise)
1238 {
1239 GDK_NOTE (DND, g_print ("idataobject_enumdadvise %p OLE_E_ADVISENOTSUPPORTED\n", This));
1240
1241 return OLE_E_ADVISENOTSUPPORTED;
1242 }
1243
1244 static ULONG STDMETHODCALLTYPE
1245 ienumformatetc_addref (LPENUMFORMATETC This)
1246 {
1247 enum_formats *en = (enum_formats *) This;
1248 int ref_count = ++en->ref_count;
1249
1250 GDK_NOTE (DND, g_print ("ienumformatetc_addref %p %d\n", This, ref_count));
1251
1252 return ref_count;
1253 }
1254
1255 static HRESULT STDMETHODCALLTYPE
1256 ienumformatetc_queryinterface (LPENUMFORMATETC This,
1257 REFIID riid,
1258 LPVOID *ppvObject)
1259 {
1260 GDK_NOTE (DND, {
1261 g_print ("ienumformatetc_queryinterface %p", This);
1262 PRINT_GUID (riid);
1263 });
1264
1265 *ppvObject = NULL;
1266
1267 if (IsEqualGUID (riid, &IID_IUnknown))
1268 {
1269 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
1270 ienumformatetc_addref (This);
1271 *ppvObject = This;
1272 return S_OK;
1273 }
1274 else if (IsEqualGUID (riid, &IID_IEnumFORMATETC))
1275 {
1276 GDK_NOTE (DND, g_print ("...IEnumFORMATETC S_OK\n"));
1277 ienumformatetc_addref (This);
1278 *ppvObject = This;
1279 return S_OK;
1280 }
1281 else
1282 {
1283 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
1284 return E_NOINTERFACE;
1285 }
1286 }
1287
1288 static ULONG STDMETHODCALLTYPE
1289 ienumformatetc_release (LPENUMFORMATETC This)
1290 {
1291 enum_formats *en = (enum_formats *) This;
1292 int ref_count = --en->ref_count;
1293
1294 GDK_NOTE (DND, g_print ("ienumformatetc_release %p %d\n", This, ref_count));
1295
1296 if (ref_count == 0)
1297 {
1298 idataobject_release ((LPDATAOBJECT) en->dataobj);
1299 g_free (This);
1300 }
1301
1302 return ref_count;
1303 }
1304
1305 static HRESULT STDMETHODCALLTYPE
1306 ienumformatetc_next (LPENUMFORMATETC This,
1307 ULONG celt,
1308 LPFORMATETC elts,
1309 ULONG *nelt)
1310 {
1311 enum_formats *en = (enum_formats *) This;
1312 ULONG i, n;
1313 ULONG formats_to_get = celt;
1314
1315 GDK_NOTE (DND, g_print ("ienumformatetc_next %p %d %ld ", This, en->ix, celt));
1316
1317 n = 0;
1318 for (i = 0; i < formats_to_get; i++)
1319 {
1320 UINT fmt;
1321 if (en->ix >= en->dataobj->formats->len)
1322 break;
1323 fmt = g_array_index (en->dataobj->formats, GdkSelTargetFormat, en->ix++).format;
1324 /* skip internals */
1325 if (fmt == 0 || fmt > 0xFFFF)
1326 {
1327 formats_to_get += 1;
1328 continue;
1329 }
1330 elts[n].cfFormat = fmt;
1331 elts[n].ptd = NULL;
1332 elts[n].dwAspect = DVASPECT_CONTENT;
1333 elts[n].lindex = -1;
1334 elts[n].tymed = TYMED_HGLOBAL;
1335
1336 n++;
1337 }
1338
1339 if (nelt != NULL)
1340 *nelt = n;
1341
1342 GDK_NOTE (DND, g_print ("%s\n", (n == celt) ? "S_OK" : "S_FALSE"));
1343
1344 if (n == celt)
1345 return S_OK;
1346 else
1347 return S_FALSE;
1348 }
1349
1350 static HRESULT STDMETHODCALLTYPE
1351 ienumformatetc_skip (LPENUMFORMATETC This,
1352 ULONG celt)
1353 {
1354 enum_formats *en = (enum_formats *) This;
1355
1356 GDK_NOTE (DND, g_print ("ienumformatetc_skip %p %d %ld S_OK\n", This, en->ix, celt));
1357
1358 en->ix += celt;
1359
1360 return S_OK;
1361 }
1362
1363 static HRESULT STDMETHODCALLTYPE
1364 ienumformatetc_reset (LPENUMFORMATETC This)
1365 {
1366 enum_formats *en = (enum_formats *) This;
1367
1368 GDK_NOTE (DND, g_print ("ienumformatetc_reset %p S_OK\n", This));
1369
1370 en->ix = 0;
1371
1372 return S_OK;
1373 }
1374
1375 static HRESULT STDMETHODCALLTYPE
1376 ienumformatetc_clone (LPENUMFORMATETC This,
1377 LPENUMFORMATETC *ppEnumFormatEtc)
1378 {
1379 enum_formats *en = (enum_formats *) This;
1380 enum_formats *new;
1381
1382 GDK_NOTE (DND, g_print ("ienumformatetc_clone %p S_OK\n", This));
1383
1384 new = enum_formats_new (en->dataobj);
1385
1386 new->ix = en->ix;
1387
1388 *ppEnumFormatEtc = &new->ief;
1389
1390 return S_OK;
1391 }
1392
1393 static IDropTargetVtbl idt_vtbl = {
1394 idroptarget_queryinterface,
1395 idroptarget_addref,
1396 idroptarget_release,
1397 idroptarget_dragenter,
1398 idroptarget_dragover,
1399 idroptarget_dragleave,
1400 idroptarget_drop
1401 };
1402
1403 static IDropSourceVtbl ids_vtbl = {
1404 idropsource_queryinterface,
1405 idropsource_addref,
1406 idropsource_release,
1407 idropsource_querycontinuedrag,
1408 idropsource_givefeedback
1409 };
1410
1411 static IDataObjectVtbl ido_vtbl = {
1412 idataobject_queryinterface,
1413 idataobject_addref,
1414 idataobject_release,
1415 idataobject_getdata,
1416 idataobject_getdatahere,
1417 idataobject_querygetdata,
1418 idataobject_getcanonicalformatetc,
1419 idataobject_setdata,
1420 idataobject_enumformatetc,
1421 idataobject_dadvise,
1422 idataobject_dunadvise,
1423 idataobject_enumdadvise
1424 };
1425
1426 static IEnumFORMATETCVtbl ief_vtbl = {
1427 ienumformatetc_queryinterface,
1428 ienumformatetc_addref,
1429 ienumformatetc_release,
1430 ienumformatetc_next,
1431 ienumformatetc_skip,
1432 ienumformatetc_reset,
1433 ienumformatetc_clone
1434 };
1435
1436
1437 static target_drag_context *
1438 target_context_new (GdkWindow *window)
1439 {
1440 target_drag_context *result;
1441
1442 result = g_new0 (target_drag_context, 1);
1443 result->idt.lpVtbl = &idt_vtbl;
1444 result->ref_count = 0;
1445
1446 result->dest_window = g_object_ref (window);
1447
1448 idroptarget_addref (&result->idt);
1449
1450 GDK_NOTE (DND, g_print ("target_context_new: %p (window %p)\n", result, result->dest_window));
1451
1452 return result;
1453 }
1454
1455 static source_drag_context *
1456 source_context_new (GdkWindow *window,
1457 GList *targets)
1458 {
1459 GdkDragContext *context;
1460 GdkWin32DragContext *context_win32;
1461 source_drag_context *result;
1462 GList *p;
1463
1464 context = gdk_drag_context_new (gdk_window_get_display (window));
1465 context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
1466
1467 result = g_new0 (source_drag_context, 1);
1468 result->context = context;
1469 result->ids.lpVtbl = &ids_vtbl;
1470 result->ref_count = 0;
1471
1472 result->context->protocol = GDK_DRAG_PROTO_OLE2;
1473 result->context->is_source = TRUE;
1474
1475 result->context->source_window = g_object_ref (window);
1476
1477 result->context->dest_window = NULL;
1478 result->context->targets = g_list_copy (targets);
1479 context_win32->has_image_format = FALSE;
1480 context_win32->has_cf_png = FALSE;
1481 context_win32->has_cf_dib = FALSE;
1482 context_win32->has_text_uri_list = FALSE;
1483 context_win32->has_shell_id_list = FALSE;
1484 context_win32->has_unicodetext = FALSE;
1485
1486 for (p = result->context->targets; p; p = g_list_next (p))
1487 {
1488 gint j;
1489 GdkAtom target = p->data;
1490
1491 if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST))
1492 context_win32->has_text_uri_list = TRUE;
1493 else if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_CFSTR_SHELLIDLIST))
1494 context_win32->has_shell_id_list = TRUE;
1495 else if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_PNG))
1496 context_win32->has_cf_png = TRUE;
1497 else if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_CF_DIB))
1498 context_win32->has_cf_dib = TRUE;
1499 else if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GIF))
1500 context_win32->has_gif = TRUE;
1501 else if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_JFIF))
1502 context_win32->has_jfif = TRUE;
1503 else if (target == _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_CF_UNICODETEXT))
1504 context_win32->has_unicodetext = TRUE;
1505
1506 for (j = 0; !context_win32->has_image_format && j < _gdk_win32_selection_get ()->n_known_pixbuf_formats; j++)
1507 if (target == _gdk_win32_selection_get ()->known_pixbuf_formats[j])
1508 context_win32->has_image_format = TRUE;
1509 }
1510
1511 idropsource_addref (&result->ids);
1512
1513 GDK_NOTE (DND, g_print ("source_context_new: %p (drag context %p)\n", result, result->context));
1514
1515 if (current_src_context == NULL)
1516 current_src_context = result;
1517
1518 return result;
1519 }
1520
1521 static data_object *
1522 data_object_new (GdkDragContext *context)
1523 {
1524 data_object *result;
1525 GList *p;
1526
1527 result = g_new0 (data_object, 1);
1528
1529 result->ido.lpVtbl = &ido_vtbl;
1530 result->ref_count = 1;
1531 result->context = context;
1532 result->formats = g_array_new (FALSE, FALSE, sizeof (GdkSelTargetFormat));
1533
1534 for (p = context->targets; p; p = p->next)
1535 {
1536 GdkAtom target = p->data;
1537 gchar *target_name = gdk_atom_name (target);
1538 gint added_count = 0;
1539 gint i;
1540
1541 GDK_NOTE (DND, g_print ("DataObject supports target 0x%p (%s)\n", target, target_name));
1542 g_free (target_name);
1543
1544 added_count = _gdk_win32_add_target_to_selformats (target, result->formats);
1545
1546 for (i = 0; i < added_count && result->formats->len - 1 - i >= 0; i++)
1547 GDK_NOTE (DND, g_print ("DataObject will support format 0x%x\n", g_array_index (result->formats, GdkSelTargetFormat, i).format));
1548 }
1549
1550 GDK_NOTE (DND, g_print ("data_object_new: %p\n", result));
1551
1552 return result;
1553 }
1554
1555 static enum_formats *
1556 enum_formats_new (data_object *dataobj)
1557 {
1558 enum_formats *result;
1559
1560 result = g_new0 (enum_formats, 1);
1561
1562 result->ief.lpVtbl = &ief_vtbl;
1563 result->ref_count = 1;
1564 result->ix = 0;
1565 result->dataobj = dataobj;
1566 idataobject_addref ((LPDATAOBJECT) dataobj);
1567
1568 return result;
1569 }
1570
1571 /* From MS Knowledge Base article Q130698 */
1572
1573 static gboolean
1574 resolve_link (HWND hWnd,
1575 wchar_t *link,
1576 gchar **lpszPath)
1577 {
1578 WIN32_FILE_ATTRIBUTE_DATA wfad;
1579 HRESULT hr;
1580 IShellLinkW *pslW = NULL;
1581 IPersistFile *ppf = NULL;
1582
1583 /* Check if the file is empty first because IShellLink::Resolve for
1584 * some reason succeeds with an empty file and returns an empty
1585 * "link target". (#524151)
1586 */
1587 if (!GetFileAttributesExW (link, GetFileExInfoStandard, &wfad) ||
1588 (wfad.nFileSizeHigh == 0 && wfad.nFileSizeLow == 0))
1589 return FALSE;
1590
1591 /* Assume failure to start with: */
1592 *lpszPath = 0;
1593
1594 /* Call CoCreateInstance to obtain the IShellLink interface
1595 * pointer. This call fails if CoInitialize is not called, so it is
1596 * assumed that CoInitialize has been called.
1597 */
1598
1599 hr = CoCreateInstance (&CLSID_ShellLink,
1600 NULL,
1601 CLSCTX_INPROC_SERVER,
1602 &IID_IShellLinkW,
1603 (LPVOID *)&pslW);
1604
1605 if (SUCCEEDED (hr))
1606 {
1607 /* The IShellLink interface supports the IPersistFile
1608 * interface. Get an interface pointer to it.
1609 */
1610 hr = pslW->lpVtbl->QueryInterface (pslW,
1611 &IID_IPersistFile,
1612 (LPVOID *) &ppf);
1613 }
1614
1615 if (SUCCEEDED (hr))
1616 {
1617 /* Load the file. */
1618 hr = ppf->lpVtbl->Load (ppf, link, STGM_READ);
1619 }
1620
1621 if (SUCCEEDED (hr))
1622 {
1623 /* Resolve the link by calling the Resolve()
1624 * interface function.
1625 */
1626 hr = pslW->lpVtbl->Resolve (pslW, hWnd, SLR_ANY_MATCH | SLR_NO_UI);
1627 }
1628
1629 if (SUCCEEDED (hr))
1630 {
1631 wchar_t wtarget[MAX_PATH];
1632
1633 hr = pslW->lpVtbl->GetPath (pslW, wtarget, MAX_PATH, NULL, 0);
1634 if (SUCCEEDED (hr))
1635 *lpszPath = g_utf16_to_utf8 (wtarget, -1, NULL, NULL, NULL);
1636 }
1637
1638 if (ppf)
1639 ppf->lpVtbl->Release (ppf);
1640
1641 if (pslW)
1642 pslW->lpVtbl->Release (pslW);
1643
1644 return SUCCEEDED (hr);
1645 }
1646
1647 #if 0
1648
1649 /* Check for filenames like C:\Users\tml\AppData\Local\Temp\d5qtkvvs.bmp */
1650 static gboolean
1651 filename_looks_tempish (const char *filename)
1652 {
1653 char *dirname;
1654 char *p;
1655 const char *q;
1656 gboolean retval = FALSE;
1657
1658 dirname = g_path_get_dirname (filename);
1659
1660 p = dirname;
1661 q = g_get_tmp_dir ();
1662
1663 while (*p && *q &&
1664 ((G_IS_DIR_SEPARATOR (*p) && G_IS_DIR_SEPARATOR (*q)) ||
1665 g_ascii_tolower (*p) == g_ascii_tolower (*q)))
1666 p++, q++;
1667
1668 if (!*p && !*q)
1669 retval = TRUE;
1670
1671 g_free (dirname);
1672
1673 return retval;
1674 }
1675
1676 static gboolean
1677 close_it (gpointer data)
1678 {
1679 close (GPOINTER_TO_INT (data));
1680
1681 return FALSE;
1682 }
1683
1684 #endif
1685
1686 static GdkFilterReturn
1687 gdk_dropfiles_filter (GdkXEvent *xev,
1688 GdkEvent *event,
1689 gpointer data)
1690 {
1691 GdkDragContext *context;
1692 GdkWin32DragContext *context_win32;
1693 GString *result;
1694 MSG *msg = (MSG *) xev;
1695 HANDLE hdrop;
1696 POINT pt;
1697 gint nfiles, i;
1698 gchar *fileName, *linkedFile;
1699
1700 if (msg->message == WM_DROPFILES)
1701 {
1702 GDK_NOTE (DND, g_print ("WM_DROPFILES: %p\n", msg->hwnd));
1703
1704 context = gdk_drag_context_new (gdk_window_get_display (event->any.window));
1705 context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
1706 context->protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
1707 context->is_source = FALSE;
1708
1709 context->source_window = gdk_get_default_root_window ();
1710 g_object_ref (context->source_window);
1711
1712 g_set_object (&context->dest_window, event->any.window);
1713
1714 /* WM_DROPFILES drops are always file names */
1715 context->targets =
1716 g_list_append (NULL, _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_TEXT_URI_LIST));
1717 context->actions = GDK_ACTION_COPY;
1718 context->suggested_action = GDK_ACTION_COPY;
1719 current_dest_drag = context;
1720
1721 event->dnd.type = GDK_DROP_START;
1722 event->dnd.context = current_dest_drag;
1723 gdk_event_set_device (event, gdk_drag_context_get_device (current_dest_drag));
1724 gdk_event_set_seat (event, gdk_device_get_seat (gdk_drag_context_get_device (current_dest_drag)));
1725
1726 hdrop = (HANDLE) msg->wParam;
1727 DragQueryPoint (hdrop, &pt);
1728 ClientToScreen (msg->hwnd, &pt);
1729
1730 event->dnd.x_root = (pt.x + _gdk_offset_x) / context_win32->scale;
1731 event->dnd.y_root = (pt.y + _gdk_offset_y) / context_win32->scale;
1732 event->dnd.time = _gdk_win32_get_next_tick (msg->time);
1733
1734 nfiles = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
1735
1736 result = g_string_new (NULL);
1737 for (i = 0; i < nfiles; i++)
1738 {
1739 gchar *uri;
1740 wchar_t wfn[MAX_PATH];
1741
1742 DragQueryFileW (hdrop, i, wfn, MAX_PATH);
1743 fileName = g_utf16_to_utf8 (wfn, -1, NULL, NULL, NULL);
1744
1745 /* Resolve shortcuts */
1746 if (resolve_link (msg->hwnd, wfn, &linkedFile))
1747 {
1748 uri = g_filename_to_uri (linkedFile, NULL, NULL);
1749 if (uri != NULL)
1750 {
1751 g_string_append (result, uri);
1752 GDK_NOTE (DND, g_print ("... %s link to %s: %s\n",
1753 fileName, linkedFile, uri));
1754 g_free (uri);
1755 }
1756 g_free (fileName);
1757 fileName = linkedFile;
1758 }
1759 else
1760 {
1761 uri = g_filename_to_uri (fileName, NULL, NULL);
1762 if (uri != NULL)
1763 {
1764 g_string_append (result, uri);
1765 GDK_NOTE (DND, g_print ("... %s: %s\n", fileName, uri));
1766 g_free (uri);
1767 }
1768 }
1769
1770 #if 0
1771 /* Awful hack to recognize temp files corresponding to
1772 * images dragged from Firefox... Open the file right here
1773 * so that it is less likely that Firefox manages to delete
1774 * it before the GTK+-using app (typically GIMP) has opened
1775 * it.
1776 *
1777 * Not compiled in for now, because it means images dragged
1778 * from Firefox would stay around in the temp folder which
1779 * is not what Firefox intended. I don't feel comfortable
1780 * with that, both from a geenral sanity point of view, and
1781 * from a privacy point of view. It's better to wait for
1782 * Firefox to fix the problem, for instance by deleting the
1783 * temp file after a longer delay, or to wait until we
1784 * implement the OLE2_DND...
1785 */
1786 if (filename_looks_tempish (fileName))
1787 {
1788 int fd = g_open (fileName, _O_RDONLY|_O_BINARY, 0);
1789 if (fd == -1)
1790 {
1791 GDK_NOTE (DND, g_print ("Could not open %s, maybe an image dragged from Firefox that it already deleted\n", fileName));
1792 }
1793 else
1794 {
1795 GDK_NOTE (DND, g_print ("Opened %s as %d so that Firefox won't delete it\n", fileName, fd));
1796 g_timeout_add_seconds (1, close_it, GINT_TO_POINTER (fd));
1797 }
1798 }
1799 #endif
1800
1801 g_free (fileName);
1802 g_string_append (result, "\015\012");
1803 }
1804 _gdk_dropfiles_store (result->str);
1805 g_string_free (result, FALSE);
1806
1807 DragFinish (hdrop);
1808
1809 return GDK_FILTER_TRANSLATE;
1810 }
1811 else
1812 return GDK_FILTER_CONTINUE;
1813 }
1814
1815
1816
1817 void
1818 _gdk_dnd_init (void)
1819 {
1820 CoInitializeEx (NULL, COINIT_APARTMENTTHREADED);
1821
1822 if (getenv ("GDK_WIN32_USE_EXPERIMENTAL_OLE2_DND"))
1823 use_ole2_dnd = TRUE;
1824
1825 if (use_ole2_dnd)
1826 {
1827 HRESULT hr;
1828
1829 hr = OleInitialize (NULL);
1830
1831 if (! SUCCEEDED (hr))
1832 g_error ("OleInitialize failed");
1833
1834 target_ctx_for_window = g_hash_table_new (g_direct_hash, g_direct_equal);
1835 }
1836 }
1837
1838 void
1839 _gdk_win32_dnd_exit (void)
1840 {
1841 if (use_ole2_dnd)
1842 {
1843 OleUninitialize ();
1844 }
1845
1846 CoUninitialize ();
1847 }
1848
1849 /* Source side */
1850
1851 static void
1852 local_send_leave (GdkDragContext *context,
1853 guint32 time)
1854 {
1855 GdkEvent *tmp_event;
1856
1857 GDK_NOTE (DND, g_print ("local_send_leave: context=%p current_dest_drag=%p\n",
1858 context,
1859 current_dest_drag));
1860
1861 if ((current_dest_drag != NULL) &&
1862 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1863 (current_dest_drag->source_window == context->source_window))
1864 {
1865 tmp_event = gdk_event_new (GDK_DRAG_LEAVE);
1866
1867 g_set_object (&tmp_event->dnd.window, context->dest_window);
1868 /* Pass ownership of context to the event */
1869 tmp_event->dnd.send_event = FALSE;
1870 g_set_object (&tmp_event->dnd.context, current_dest_drag);
1871 tmp_event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1872 gdk_event_set_device (tmp_event, gdk_drag_context_get_device (context));
1873 gdk_event_set_seat (tmp_event, gdk_device_get_seat (gdk_drag_context_get_device (context)));
1874
1875 current_dest_drag = NULL;
1876
1877 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
1878 gdk_event_put (tmp_event);
1879 gdk_event_free (tmp_event);
1880 }
1881 }
1882
1883 static void
1884 local_send_enter (GdkDragContext *context,
1885 guint32 time)
1886 {
1887 GdkEvent *tmp_event;
1888 GdkDragContext *new_context;
1889
1890 GDK_NOTE (DND, g_print ("local_send_enter: context=%p current_dest_drag=%p\n",
1891 context,
1892 current_dest_drag));
1893
1894 if (current_dest_drag != NULL)
1895 {
1896 g_object_unref (G_OBJECT (current_dest_drag));
1897 current_dest_drag = NULL;
1898 }
1899
1900 new_context = gdk_drag_context_new (gdk_window_get_display (context->source_window));
1901 new_context->protocol = GDK_DRAG_PROTO_LOCAL;
1902 new_context->is_source = FALSE;
1903
1904 g_set_object (&new_context->source_window, context->source_window);
1905 g_set_object (&new_context->dest_window, context->dest_window);
1906
1907 new_context->targets = g_list_copy (context->targets);
1908
1909 gdk_window_set_events (new_context->source_window,
1910 gdk_window_get_events (new_context->source_window) |
1911 GDK_PROPERTY_CHANGE_MASK);
1912 new_context->actions = context->actions;
1913
1914 tmp_event = gdk_event_new (GDK_DRAG_ENTER);
1915 g_set_object (&tmp_event->dnd.window, context->dest_window);
1916 tmp_event->dnd.send_event = FALSE;
1917 g_set_object (&tmp_event->dnd.context, new_context);
1918 tmp_event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1919 gdk_event_set_device (tmp_event, gdk_drag_context_get_device (context));
1920 gdk_event_set_seat (tmp_event, gdk_device_get_seat (gdk_drag_context_get_device (context)));
1921
1922 current_dest_drag = new_context;
1923
1924 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
1925 gdk_event_put (tmp_event);
1926 gdk_event_free (tmp_event);
1927 }
1928
1929 static void
1930 local_send_motion (GdkDragContext *context,
1931 gint x_root,
1932 gint y_root,
1933 GdkDragAction action,
1934 guint32 time)
1935 {
1936 GdkEvent *tmp_event;
1937 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
1938
1939 GDK_NOTE (DND, g_print ("local_send_motion: context=%p (%d,%d) current_dest_drag=%p\n",
1940 context, x_root, y_root,
1941 current_dest_drag));
1942
1943 if ((current_dest_drag != NULL) &&
1944 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1945 (current_dest_drag->source_window == context->source_window))
1946 {
1947 GdkWin32DragContext *current_dest_drag_win32;
1948
1949 tmp_event = gdk_event_new (GDK_DRAG_MOTION);
1950 g_set_object (&tmp_event->dnd.window, current_dest_drag->dest_window);
1951 tmp_event->dnd.send_event = FALSE;
1952 g_set_object (&tmp_event->dnd.context, current_dest_drag);
1953 tmp_event->dnd.time = time;
1954 gdk_event_set_device (tmp_event, gdk_drag_context_get_device (current_dest_drag));
1955 gdk_event_set_seat (tmp_event, gdk_device_get_seat (gdk_drag_context_get_device (current_dest_drag)));
1956
1957 current_dest_drag->suggested_action = action;
1958
1959 tmp_event->dnd.x_root = x_root;
1960 tmp_event->dnd.y_root = y_root;
1961
1962 current_dest_drag_win32 = GDK_WIN32_DRAG_CONTEXT (current_dest_drag);
1963 current_dest_drag_win32->last_x = x_root;
1964 current_dest_drag_win32->last_y = y_root;
1965
1966 context_win32->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1967
1968 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
1969 gdk_event_put (tmp_event);
1970 gdk_event_free (tmp_event);
1971 }
1972 }
1973
1974 static void
1975 local_send_drop (GdkDragContext *context,
1976 guint32 time)
1977 {
1978 GdkEvent *tmp_event;
1979
1980 GDK_NOTE (DND, g_print ("local_send_drop: context=%p current_dest_drag=%p\n",
1981 context,
1982 current_dest_drag));
1983
1984 if ((current_dest_drag != NULL) &&
1985 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1986 (current_dest_drag->source_window == context->source_window))
1987 {
1988 GdkWin32DragContext *context_win32;
1989
1990 /* Pass ownership of context to the event */
1991 tmp_event = gdk_event_new (GDK_DROP_START);
1992 g_set_object (&tmp_event->dnd.window, current_dest_drag->dest_window);
1993 tmp_event->dnd.send_event = FALSE;
1994 g_set_object (&tmp_event->dnd.context, current_dest_drag);
1995 tmp_event->dnd.time = GDK_CURRENT_TIME;
1996 gdk_event_set_device (tmp_event, gdk_drag_context_get_device (current_dest_drag));
1997 gdk_event_set_seat (tmp_event, gdk_device_get_seat (gdk_drag_context_get_device (current_dest_drag)));
1998
1999 context_win32 = GDK_WIN32_DRAG_CONTEXT (current_dest_drag);
2000 tmp_event->dnd.x_root = context_win32->last_x;
2001 tmp_event->dnd.y_root = context_win32->last_y;
2002
2003 current_dest_drag = NULL;
2004
2005 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2006 gdk_event_put (tmp_event);
2007 gdk_event_free (tmp_event);
2008 }
2009
2010 }
2011
2012 static void
2013 gdk_drag_do_leave (GdkDragContext *context,
2014 guint32 time)
2015 {
2016 if (context->dest_window)
2017 {
2018 GDK_NOTE (DND, g_print ("gdk_drag_do_leave\n"));
2019
2020 if (!use_ole2_dnd)
2021 {
2022 if (context->protocol == GDK_DRAG_PROTO_LOCAL)
2023 local_send_leave (context, time);
2024 }
2025
2026 g_clear_object (&context->dest_window);
2027 }
2028 }
2029
2030 static GdkWindow *
2031 create_drag_window (GdkScreen *screen)
2032 {
2033 GdkWindowAttr attrs = { 0 };
2034 guint mask;
2035
2036 attrs.x = attrs.y = 0;
2037 attrs.width = attrs.height = 100;
2038 attrs.wclass = GDK_INPUT_OUTPUT;
2039 attrs.window_type = GDK_WINDOW_TEMP;
2040 attrs.type_hint = GDK_WINDOW_TYPE_HINT_DND;
2041 attrs.visual = gdk_screen_get_rgba_visual (screen);
2042 if (!attrs.visual)
2043 attrs.visual = gdk_screen_get_system_visual (screen);
2044
2045 mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_TYPE_HINT;
2046
2047 return gdk_window_new (gdk_screen_get_root_window (screen), &attrs, mask);
2048 }
2049
2050 GdkDragContext *
2051 _gdk_win32_window_drag_begin (GdkWindow *window,
2052 GdkDevice *device,
2053 GList *targets,
2054 gint x_root,
2055 gint y_root)
2056 {
2057 GdkDragContext *new_context;
2058 GdkWin32DragContext *context_win32;
2059 BYTE kbd_state[256];
2060 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
2061
2062 if (!use_ole2_dnd)
2063 {
2064 g_return_val_if_fail (window != NULL, NULL);
2065
2066 new_context = gdk_drag_context_new (gdk_window_get_display (window));
2067
2068 new_context->is_source = TRUE;
2069 g_set_object (&new_context->source_window, window);
2070
2071 new_context->targets = g_list_copy (targets);
2072 new_context->actions = 0;
2073 context_win32 = GDK_WIN32_DRAG_CONTEXT (new_context);
2074 }
2075 else
2076 {
2077 source_drag_context *ctx;
2078
2079 g_return_val_if_fail (window != NULL, NULL);
2080
2081 GDK_NOTE (DND, g_print ("gdk_drag_begin\n"));
2082
2083 ctx = source_context_new (window, targets);
2084
2085 sel_win32->dnd_source_state = GDK_WIN32_DND_PENDING;
2086
2087 pending_src_context = ctx;
2088 new_context = g_object_ref (ctx->context);
2089
2090 context_win32 = GDK_WIN32_DRAG_CONTEXT (new_context);
2091 }
2092
2093 context_win32->start_x = x_root;
2094 context_win32->start_y = y_root;
2095 context_win32->last_x = context_win32->start_x;
2096 context_win32->last_y = context_win32->start_y;
2097
2098 context_win32->last_key_state = 0;
2099 API_CALL (GetKeyboardState, (kbd_state));
2100
2101 if (kbd_state[VK_MENU] & 0x80)
2102 context_win32->last_key_state |= MK_ALT;
2103 if (kbd_state[VK_CONTROL] & 0x80)
2104 context_win32->last_key_state |= MK_CONTROL;
2105 if (kbd_state[VK_SHIFT] & 0x80)
2106 context_win32->last_key_state |= MK_SHIFT;
2107 if (kbd_state[VK_LBUTTON] & 0x80)
2108 context_win32->last_key_state |= MK_LBUTTON;
2109 if (kbd_state[VK_MBUTTON] & 0x80)
2110 context_win32->last_key_state |= MK_MBUTTON;
2111 if (kbd_state[VK_RBUTTON] & 0x80)
2112 context_win32->last_key_state |= MK_RBUTTON;
2113
2114 context_win32->drag_window = create_drag_window (gdk_window_get_screen (window));
2115
2116 return new_context;
2117 }
2118
2119 void
2120 _gdk_win32_dnd_do_dragdrop (void)
2121 {
2122 GdkDragContext* drag_ctx;
2123 data_object *dobj;
2124 HRESULT hr;
2125 DWORD dwEffect;
2126
2127 if (!use_ole2_dnd)
2128 return;
2129
2130 if (pending_src_context == NULL)
2131 return;
2132
2133 drag_ctx = pending_src_context->context;
2134
2135 dobj = data_object_new (drag_ctx);
2136 current_src_object = dobj;
2137
2138 /* Start dragging with mainloop inside the OLE2 API. Exits only when done */
2139
2140 GDK_NOTE (DND, g_print ("Calling DoDragDrop\n"));
2141
2142 _gdk_win32_begin_modal_call (GDK_WIN32_MODAL_OP_DND);
2143 hr = DoDragDrop (&dobj->ido, &pending_src_context->ids,
2144 DROPEFFECT_COPY | DROPEFFECT_MOVE,
2145 &dwEffect);
2146 _gdk_win32_end_modal_call (GDK_WIN32_MODAL_OP_DND);
2147
2148 GDK_NOTE (DND, g_print ("DoDragDrop returned %s\n",
2149 (hr == DRAGDROP_S_DROP ? "DRAGDROP_S_DROP" :
2150 (hr == DRAGDROP_S_CANCEL ? "DRAGDROP_S_CANCEL" :
2151 (hr == E_UNEXPECTED ? "E_UNEXPECTED" :
2152 g_strdup_printf ("%#.8lx", hr))))));
2153
2154 /* Delete dnd selection after successful move */
2155 if (hr == DRAGDROP_S_DROP &&
2156 dwEffect == DROPEFFECT_MOVE &&
2157 (drag_ctx->actions & GDK_ACTION_MOVE))
2158 {
2159 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
2160 GdkEvent tmp_event;
2161
2162 memset (&tmp_event, 0, sizeof (tmp_event));
2163 tmp_event.type = GDK_SELECTION_REQUEST;
2164 g_set_object (&tmp_event.selection.window, drag_ctx->source_window);
2165 tmp_event.selection.send_event = FALSE;
2166 tmp_event.selection.selection = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND);
2167 tmp_event.selection.target = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_DELETE);
2168 win32_sel->property_change_target_atom = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_DELETE);
2169 tmp_event.selection.property = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND);
2170 g_set_object (&tmp_event.selection.requestor, drag_ctx->source_window);
2171 tmp_event.selection.time = GDK_CURRENT_TIME; /* ??? */
2172
2173 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
2174 gdk_event_put (&tmp_event);
2175 }
2176
2177 {
2178 GdkEvent *tmp_event;
2179 tmp_event = gdk_event_new (GDK_DROP_FINISHED);
2180 g_set_object (&tmp_event->dnd.window, drag_ctx->source_window);
2181 tmp_event->dnd.send_event = FALSE;
2182 g_set_object (&tmp_event->dnd.context, drag_ctx);
2183 gdk_event_set_device (tmp_event, gdk_drag_context_get_device (drag_ctx));
2184 gdk_event_set_seat (tmp_event, gdk_device_get_seat (gdk_drag_context_get_device (drag_ctx)));
2185
2186 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2187 gdk_event_put (tmp_event);
2188 gdk_event_free (tmp_event);
2189 }
2190
2191 current_src_object = NULL;
2192 dobj->ido.lpVtbl->Release (&dobj->ido);
2193 if (pending_src_context != NULL)
2194 {
2195 pending_src_context->ids.lpVtbl->Release (&pending_src_context->ids);
2196 pending_src_context = NULL;
2197 }
2198 }
2199
2200 /* Untested, may not work ...
2201 * ... but as of this writing is only used by exlusive X11 gtksocket.c
2202 */
2203 GdkDragProtocol
2204 _gdk_win32_window_get_drag_protocol (GdkWindow *window,
2205 GdkWindow **target)
2206 {
2207 GdkDragProtocol protocol = GDK_DRAG_PROTO_NONE;
2208
2209 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2210 {
2211 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2212 {
2213 if (use_ole2_dnd)
2214 protocol = GDK_DRAG_PROTO_OLE2;
2215 else
2216 protocol = GDK_DRAG_PROTO_LOCAL;
2217 }
2218 }
2219
2220 if (target)
2221 {
2222 *target = NULL;
2223 }
2224
2225 return protocol;
2226 }
2227
2228 static GdkWindow *
2229 gdk_win32_drag_context_find_window (GdkDragContext *context,
2230 GdkWindow *drag_window,
2231 GdkScreen *screen,
2232 gint x_root,
2233 gint y_root,
2234 GdkDragProtocol *protocol)
2235 {
2236 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
2237 GdkWindow *toplevel = NULL;
2238 HWND hwnd = NULL;
2239 POINT pt;
2240
2241 pt.x = x_root * context_win32->scale - _gdk_offset_x;
2242 pt.y = y_root * context_win32->scale - _gdk_offset_y;
2243
2244 GDK_NOTE (DND,
2245 g_print ("gdk_drag_find_window_real: %+d%+d\n",
2246 (int) pt.x, (int) pt.y));
2247
2248 hwnd = WindowFromPoint (pt);
2249
2250 if (hwnd)
2251 {
2252 GdkWindow *window = gdk_win32_handle_table_lookup (hwnd);
2253
2254 if (window)
2255 {
2256 toplevel = gdk_window_get_toplevel (window);
2257 g_object_ref (toplevel);
2258 }
2259 else
2260 toplevel = gdk_win32_window_foreign_new_for_display (gdk_screen_get_display (screen), hwnd);
2261 }
2262
2263 if (use_ole2_dnd)
2264 *protocol = GDK_DRAG_PROTO_OLE2;
2265 else if (context->source_window)
2266 *protocol = GDK_DRAG_PROTO_LOCAL;
2267 else
2268 *protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
2269
2270 GDK_NOTE (DND,
2271 g_print ("gdk_drag_find_window: %+d%+d: %p: %p %s\n",
2272 x_root, y_root,
2273 hwnd,
2274 (toplevel ? GDK_WINDOW_HWND (toplevel) : NULL),
2275 _gdk_win32_drag_protocol_to_string (*protocol)));
2276
2277 return toplevel;
2278 }
2279
2280 static gboolean
2281 gdk_win32_drag_context_drag_motion (GdkDragContext *context,
2282 GdkWindow *dest_window,
2283 GdkDragProtocol protocol,
2284 gint x_root,
2285 gint y_root,
2286 GdkDragAction suggested_action,
2287 GdkDragAction possible_actions,
2288 guint32 time)
2289 {
2290 GdkWin32DragContext *context_win32;
2291
2292 g_return_val_if_fail (context != NULL, FALSE);
2293
2294 context->actions = possible_actions;
2295
2296 GDK_NOTE (DND, g_print ("gdk_drag_motion: @ %+d:%+d %s suggested=%s, possible=%s\n"
2297 " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2298 x_root, y_root,
2299 _gdk_win32_drag_protocol_to_string (protocol),
2300 _gdk_win32_drag_action_to_string (suggested_action),
2301 _gdk_win32_drag_action_to_string (possible_actions),
2302 context,
2303 _gdk_win32_drag_action_to_string (context->actions),
2304 _gdk_win32_drag_action_to_string (context->suggested_action),
2305 _gdk_win32_drag_action_to_string (context->action)));
2306
2307 context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
2308
2309 if (context_win32->drag_window)
2310 move_drag_window (context, x_root, y_root);
2311
2312 if (!use_ole2_dnd)
2313 {
2314 if (context->dest_window == dest_window)
2315 {
2316 GdkDragContext *dest_context;
2317
2318 dest_context = gdk_drag_context_find (FALSE,
2319 context->source_window,
2320 dest_window);
2321
2322 if (dest_context)
2323 dest_context->actions = context->actions;
2324
2325 context->suggested_action = suggested_action;
2326 }
2327 else
2328 {
2329 GdkEvent *tmp_event;
2330
2331 /* Send a leave to the last destination */
2332 gdk_drag_do_leave (context, time);
2333 context_win32->drag_status = GDK_DRAG_STATUS_DRAG;
2334
2335 /* Check if new destination accepts drags, and which protocol */
2336 if (dest_window)
2337 {
2338 g_set_object (&context->dest_window, dest_window);
2339 context->protocol = protocol;
2340
2341 switch (protocol)
2342 {
2343 case GDK_DRAG_PROTO_LOCAL:
2344 local_send_enter (context, time);
2345 break;
2346
2347 default:
2348 break;
2349 }
2350 context->suggested_action = suggested_action;
2351 }
2352 else
2353 {
2354 context->dest_window = NULL;
2355 context->action = 0;
2356 }
2357
2358 /* Push a status event, to let the client know that
2359 * the drag changed
2360 */
2361 tmp_event = gdk_event_new (GDK_DRAG_STATUS);
2362 g_set_object (&tmp_event->dnd.window, context->source_window);
2363 /* We use this to signal a synthetic status. Perhaps
2364 * we should use an extra field...
2365 */
2366 tmp_event->dnd.send_event = TRUE;
2367
2368 g_set_object (&tmp_event->dnd.context, context);
2369 tmp_event->dnd.time = time;
2370 gdk_event_set_device (tmp_event, gdk_drag_context_get_device (context));
2371 gdk_event_set_seat (tmp_event, gdk_device_get_seat (gdk_drag_context_get_device (context)));
2372
2373 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2374 gdk_event_put (tmp_event);
2375 gdk_event_free (tmp_event);
2376 }
2377
2378 /* Send a drag-motion event */
2379
2380 context_win32->last_x = x_root;
2381 context_win32->last_y = y_root;
2382
2383 if (context->dest_window)
2384 {
2385 if (context_win32->drag_status == GDK_DRAG_STATUS_DRAG)
2386 {
2387 switch (context->protocol)
2388 {
2389 case GDK_DRAG_PROTO_LOCAL:
2390 local_send_motion (context, x_root, y_root, suggested_action, time);
2391 break;
2392
2393 case GDK_DRAG_PROTO_NONE:
2394 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
2395 break;
2396
2397 default:
2398 break;
2399 }
2400 }
2401 else
2402 {
2403 GDK_NOTE (DND, g_print (" returning TRUE\n"
2404 " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2405 context,
2406 _gdk_win32_drag_action_to_string (context->actions),
2407 _gdk_win32_drag_action_to_string (context->suggested_action),
2408 _gdk_win32_drag_action_to_string (context->action)));
2409 return TRUE;
2410 }
2411 }
2412 }
2413
2414 GDK_NOTE (DND, g_print (" returning FALSE\n"
2415 " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2416 context,
2417 _gdk_win32_drag_action_to_string (context->actions),
2418 _gdk_win32_drag_action_to_string (context->suggested_action),
2419 _gdk_win32_drag_action_to_string (context->action)));
2420 return FALSE;
2421 }
2422
2423 static void
2424 gdk_win32_drag_context_drag_drop (GdkDragContext *context,
2425 guint32 time)
2426 {
2427 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
2428
2429 g_return_if_fail (context != NULL);
2430
2431 GDK_NOTE (DND, g_print ("gdk_drag_drop\n"));
2432
2433 if (!use_ole2_dnd)
2434 {
2435 if (context->dest_window &&
2436 context->protocol == GDK_DRAG_PROTO_LOCAL)
2437 local_send_drop (context, time);
2438 }
2439 else
2440 {
2441 sel_win32->dnd_source_state = GDK_WIN32_DND_DROPPED;
2442 }
2443 }
2444
2445 static void
2446 gdk_win32_drag_context_drag_abort (GdkDragContext *context,
2447 guint32 time)
2448 {
2449 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
2450
2451 g_return_if_fail (context != NULL);
2452
2453 GDK_NOTE (DND, g_print ("gdk_drag_abort\n"));
2454
2455 if (use_ole2_dnd)
2456 sel_win32->dnd_source_state = GDK_WIN32_DND_NONE;
2457 }
2458
2459 /* Destination side */
2460
2461 static void
2462 gdk_win32_drag_context_drag_status (GdkDragContext *context,
2463 GdkDragAction action,
2464 guint32 time)
2465 {
2466 GdkDragContext *src_context;
2467 GdkEvent *tmp_event;
2468
2469 g_return_if_fail (context != NULL);
2470
2471 GDK_NOTE (DND, g_print ("gdk_drag_status: %s\n"
2472 " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2473 _gdk_win32_drag_action_to_string (action),
2474 context,
2475 _gdk_win32_drag_action_to_string (context->actions),
2476 _gdk_win32_drag_action_to_string (context->suggested_action),
2477 _gdk_win32_drag_action_to_string (context->action)));
2478
2479 context->action = action;
2480
2481 if (!use_ole2_dnd)
2482 {
2483 src_context = gdk_drag_context_find (TRUE,
2484 context->source_window,
2485 context->dest_window);
2486
2487 if (src_context)
2488 {
2489 GdkWin32DragContext *src_context_win32 = GDK_WIN32_DRAG_CONTEXT (src_context);
2490
2491 if (src_context_win32->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2492 src_context_win32->drag_status = GDK_DRAG_STATUS_DRAG;
2493
2494 tmp_event = gdk_event_new (GDK_DRAG_STATUS);
2495 g_set_object (&tmp_event->dnd.window, context->source_window);
2496 tmp_event->dnd.send_event = FALSE;
2497 g_set_object (&tmp_event->dnd.context, src_context);
2498 tmp_event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2499 gdk_event_set_device (tmp_event, gdk_drag_context_get_device (src_context));
2500 gdk_event_set_seat (tmp_event, gdk_device_get_seat (gdk_drag_context_get_device (src_context)));
2501
2502 if (action == GDK_ACTION_DEFAULT)
2503 action = 0;
2504
2505 src_context->action = action;
2506
2507 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2508 gdk_event_put (tmp_event);
2509 gdk_event_free (tmp_event);
2510 }
2511 }
2512 }
2513
2514 static void
2515 gdk_win32_drag_context_drop_reply (GdkDragContext *context,
2516 gboolean ok,
2517 guint32 time)
2518 {
2519 g_return_if_fail (context != NULL);
2520
2521 GDK_NOTE (DND, g_print ("gdk_drop_reply\n"));
2522
2523 if (!use_ole2_dnd)
2524 if (context->dest_window)
2525 {
2526 if (context->protocol == GDK_DRAG_PROTO_WIN32_DROPFILES)
2527 _gdk_dropfiles_store (NULL);
2528 }
2529 }
2530
2531 static void
2532 gdk_win32_drag_context_drop_finish (GdkDragContext *context,
2533 gboolean success,
2534 guint32 time)
2535 {
2536 GdkDragContext *src_context;
2537 GdkEvent *tmp_event;
2538 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
2539
2540 g_return_if_fail (context != NULL);
2541
2542 GDK_NOTE (DND, g_print ("gdk_drop_finish\n"));
2543
2544 if (!use_ole2_dnd)
2545 {
2546 src_context = gdk_drag_context_find (TRUE,
2547 context->source_window,
2548 context->dest_window);
2549 if (src_context)
2550 {
2551 if (gdk_drag_context_get_selected_action (src_context) == GDK_ACTION_MOVE)
2552 {
2553 tmp_event = gdk_event_new (GDK_SELECTION_REQUEST);
2554 g_set_object (&tmp_event->selection.window, src_context->source_window);
2555 tmp_event->selection.send_event = FALSE;
2556 tmp_event->selection.selection = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_LOCAL_DND_SELECTION);
2557 tmp_event->selection.target = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_DELETE);
2558 sel_win32->property_change_target_atom = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_DELETE);
2559 tmp_event->selection.property = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_LOCAL_DND_SELECTION);
2560 g_set_object (&tmp_event->selection.requestor, src_context->source_window);
2561 tmp_event->selection.time = GDK_CURRENT_TIME; /* ??? */
2562
2563 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2564 gdk_event_put (tmp_event);
2565 gdk_event_free (tmp_event);
2566 }
2567
2568 tmp_event = gdk_event_new (GDK_DROP_FINISHED);
2569 g_set_object (&tmp_event->dnd.window, src_context->source_window);
2570 tmp_event->dnd.send_event = FALSE;
2571 g_set_object (&tmp_event->dnd.context, src_context);
2572 gdk_event_set_device (tmp_event, gdk_drag_context_get_device (src_context));
2573 gdk_event_set_seat (tmp_event, gdk_device_get_seat (gdk_drag_context_get_device (src_context)));
2574
2575 GDK_NOTE (EVENTS, _gdk_win32_print_event (tmp_event));
2576 gdk_event_put (tmp_event);
2577 gdk_event_free (tmp_event);
2578 }
2579 }
2580 else
2581 {
2582 gdk_drag_do_leave (context, time);
2583
2584 if (success)
2585 sel_win32->dnd_target_state = GDK_WIN32_DND_DROPPED;
2586 else
2587 sel_win32->dnd_target_state = GDK_WIN32_DND_FAILED;
2588 }
2589 }
2590
2591 #if 0
2592
2593 static GdkFilterReturn
2594 gdk_destroy_filter (GdkXEvent *xev,
2595 GdkEvent *event,
2596 gpointer data)
2597 {
2598 MSG *msg = (MSG *) xev;
2599
2600 if (msg->message == WM_DESTROY)
2601 {
2602 IDropTarget *idtp = (IDropTarget *) data;
2603
2604 GDK_NOTE (DND, g_print ("gdk_destroy_filter: WM_DESTROY: %p\n", msg->hwnd));
2605 #if 0
2606 idtp->lpVtbl->Release (idtp);
2607 #endif
2608 RevokeDragDrop (msg->hwnd);
2609 CoLockObjectExternal ((IUnknown*) idtp, FALSE, TRUE);
2610 }
2611 return GDK_FILTER_CONTINUE;
2612 }
2613
2614 #endif
2615
2616 void
2617 _gdk_win32_window_register_dnd (GdkWindow *window)
2618 {
2619 target_drag_context *ctx;
2620 HRESULT hr;
2621
2622 g_return_if_fail (window != NULL);
2623
2624 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
2625 return;
2626
2627 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2628 return;
2629 else
2630 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
2631
2632 GDK_NOTE (DND, g_print ("gdk_window_register_dnd: %p\n", GDK_WINDOW_HWND (window)));
2633
2634 if (!use_ole2_dnd)
2635 {
2636 /* We always claim to accept dropped files, but in fact we might not,
2637 * of course. This function is called in such a way that it cannot know
2638 * whether the window (widget) in question actually accepts files
2639 * (in gtk, data of type text/uri-list) or not.
2640 */
2641 gdk_window_add_filter (window, gdk_dropfiles_filter, NULL);
2642 DragAcceptFiles (GDK_WINDOW_HWND (window), TRUE);
2643 }
2644 else
2645 {
2646 /* Return if window is already setup for DND. */
2647 if (g_hash_table_lookup (target_ctx_for_window, GDK_WINDOW_HWND (window)) != NULL)
2648 return;
2649
2650 ctx = target_context_new (window);
2651
2652 hr = CoLockObjectExternal ((IUnknown *) &ctx->idt, TRUE, FALSE);
2653 if (!SUCCEEDED (hr))
2654 OTHER_API_FAILED ("CoLockObjectExternal");
2655 else
2656 {
2657 hr = RegisterDragDrop (GDK_WINDOW_HWND (window), &ctx->idt);
2658 if (hr == DRAGDROP_E_ALREADYREGISTERED)
2659 {
2660 g_print ("DRAGDROP_E_ALREADYREGISTERED\n");
2661 CoLockObjectExternal ((IUnknown *) &ctx->idt, FALSE, FALSE);
2662 }
2663 else if (!SUCCEEDED (hr))
2664 OTHER_API_FAILED ("RegisterDragDrop");
2665 else
2666 {
2667 g_object_ref (window);
2668 g_hash_table_insert (target_ctx_for_window, GDK_WINDOW_HWND (window), ctx);
2669 }
2670 }
2671 }
2672 }
2673
2674 static gboolean
2675 gdk_win32_drag_context_drop_status (GdkDragContext *context)
2676 {
2677 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
2678
2679 return ! context_win32->drop_failed;
2680 }
2681
2682 static GdkAtom
2683 gdk_win32_drag_context_get_selection (GdkDragContext *context)
2684 {
2685 switch (context->protocol)
2686 {
2687 case GDK_DRAG_PROTO_LOCAL:
2688 return _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_LOCAL_DND_SELECTION);
2689 case GDK_DRAG_PROTO_WIN32_DROPFILES:
2690 return _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_DROPFILES_DND);
2691 case GDK_DRAG_PROTO_OLE2:
2692 return _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_OLE2_DND);
2693 default:
2694 return GDK_NONE;
2695 }
2696 }
2697
2698 static void
2699 gdk_win32_drag_context_set_cursor (GdkDragContext *context,
2700 GdkCursor *cursor)
2701 {
2702 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
2703
2704 GDK_NOTE (DND, g_print ("gdk_drag_context_set_cursor: 0x%p 0x%p\n", context, cursor));
2705
2706 if (!g_set_object (&context_win32->cursor, cursor))
2707 return;
2708
2709 if (context_win32->grab_seat)
2710 {
2711 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
2712 gdk_device_grab (gdk_seat_get_pointer (context_win32->grab_seat),
2713 context_win32->ipc_window,
2714 GDK_OWNERSHIP_APPLICATION, FALSE,
2715 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
2716 cursor, GDK_CURRENT_TIME);
2717 G_GNUC_END_IGNORE_DEPRECATIONS;
2718 }
2719 }
2720
2721 static double
2722 ease_out_cubic (double t)
2723 {
2724 double p = t - 1;
2725 return p * p * p + 1;
2726 }
2727
2728 #define ANIM_TIME 500000 /* half a second */
2729
2730 typedef struct _GdkDragAnim GdkDragAnim;
2731 struct _GdkDragAnim {
2732 GdkWin32DragContext *context;
2733 GdkFrameClock *frame_clock;
2734 gint64 start_time;
2735 };
2736
2737 static void
2738 gdk_drag_anim_destroy (GdkDragAnim *anim)
2739 {
2740 g_object_unref (anim->context);
2741 g_slice_free (GdkDragAnim, anim);
2742 }
2743
2744 static gboolean
2745 gdk_drag_anim_timeout (gpointer data)
2746 {
2747 GdkDragAnim *anim = data;
2748 GdkWin32DragContext *context = anim->context;
2749 GdkFrameClock *frame_clock = anim->frame_clock;
2750 gint64 current_time;
2751 double f;
2752 double t;
2753
2754 if (!frame_clock)
2755 return G_SOURCE_REMOVE;
2756
2757 current_time = gdk_frame_clock_get_frame_time (frame_clock);
2758
2759 f = (current_time - anim->start_time) / (double) ANIM_TIME;
2760
2761 if (f >= 1.0)
2762 return G_SOURCE_REMOVE;
2763
2764 t = ease_out_cubic (f);
2765
2766 gdk_window_show (context->drag_window);
2767 gdk_window_move (context->drag_window,
2768 context->last_x + (context->start_x - context->last_x) * t - context->hot_x,
2769 context->last_y + (context->start_y - context->last_y) * t - context->hot_y);
2770 gdk_window_set_opacity (context->drag_window, 1.0 - f);
2771
2772 return G_SOURCE_CONTINUE;
2773 }
2774
2775 static void
2776 gdk_win32_drag_context_drop_done (GdkDragContext *context,
2777 gboolean success)
2778 {
2779 GdkWin32DragContext *win32_context = GDK_WIN32_DRAG_CONTEXT (context);
2780 GdkDragAnim *anim;
2781 cairo_surface_t *win_surface;
2782 cairo_surface_t *surface;
2783 cairo_pattern_t *pattern;
2784 cairo_t *cr;
2785
2786 GDK_NOTE (DND, g_print ("gdk_drag_context_drop_done: 0x%p %s\n",
2787 context,
2788 success ? "dropped successfully" : "dropped unsuccessfully"));
2789
2790 if (success)
2791 {
2792 gdk_window_hide (win32_context->drag_window);
2793 return;
2794 }
2795
2796 win_surface = _gdk_window_ref_cairo_surface (win32_context->drag_window);
2797 surface = gdk_window_create_similar_surface (win32_context->drag_window,
2798 cairo_surface_get_content (win_surface),
2799 gdk_window_get_width (win32_context->drag_window),
2800 gdk_window_get_height (win32_context->drag_window));
2801 cr = cairo_create (surface);
2802 cairo_set_source_surface (cr, win_surface, 0, 0);
2803 cairo_paint (cr);
2804 cairo_destroy (cr);
2805 cairo_surface_destroy (win_surface);
2806
2807 pattern = cairo_pattern_create_for_surface (surface);
2808
2809 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2810 gdk_window_set_background_pattern (win32_context->drag_window, pattern);
2811 G_GNUC_END_IGNORE_DEPRECATIONS
2812
2813 cairo_pattern_destroy (pattern);
2814 cairo_surface_destroy (surface);
2815
2816 anim = g_slice_new0 (GdkDragAnim);
2817 g_set_object (&anim->context, win32_context);
2818 anim->frame_clock = gdk_window_get_frame_clock (win32_context->drag_window);
2819 anim->start_time = gdk_frame_clock_get_frame_time (anim->frame_clock);
2820
2821 gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT, 17,
2822 gdk_drag_anim_timeout, anim,
2823 (GDestroyNotify) gdk_drag_anim_destroy);
2824 }
2825
2826 static gboolean
2827 drag_context_grab (GdkDragContext *context)
2828 {
2829 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
2830 GdkSeatCapabilities capabilities;
2831 GdkSeat *seat;
2832 GdkCursor *cursor;
2833
2834 if (!context_win32->ipc_window)
2835 return FALSE;
2836
2837 seat = gdk_device_get_seat (gdk_drag_context_get_device (context));
2838
2839 capabilities = GDK_SEAT_CAPABILITY_ALL;
2840
2841 cursor = gdk_drag_get_cursor (context, gdk_drag_context_get_selected_action (context));
2842 g_set_object (&context_win32->cursor, cursor);
2843
2844 if (gdk_seat_grab (seat, context_win32->ipc_window,
2845 capabilities, FALSE,
2846 context_win32->cursor, NULL, NULL, NULL) != GDK_GRAB_SUCCESS)
2847 return FALSE;
2848
2849 g_set_object (&context_win32->grab_seat, seat);
2850
2851 /* TODO: Should be grabbing keys here, to support keynav. SetWindowsHookEx()? */
2852
2853 return TRUE;
2854 }
2855
2856 static void
2857 drag_context_ungrab (GdkDragContext *context)
2858 {
2859 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
2860
2861 if (!context_win32->grab_seat)
2862 return;
2863
2864 gdk_seat_ungrab (context_win32->grab_seat);
2865
2866 g_clear_object (&context_win32->grab_seat);
2867
2868 /* TODO: Should be ungrabbing keys here */
2869 }
2870
2871 static gboolean
2872 gdk_win32_drag_context_manage_dnd (GdkDragContext *context,
2873 GdkWindow *ipc_window,
2874 GdkDragAction actions)
2875 {
2876 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
2877
2878 if (context_win32->ipc_window)
2879 return FALSE;
2880
2881 if (use_ole2_dnd)
2882 context->protocol = GDK_DRAG_PROTO_OLE2;
2883 else
2884 context->protocol = GDK_DRAG_PROTO_LOCAL;
2885
2886 g_set_object (&context_win32->ipc_window, ipc_window);
2887
2888 if (drag_context_grab (context))
2889 {
2890 context_win32->actions = actions;
2891 move_drag_window (context, context_win32->start_x, context_win32->start_y);
2892 return TRUE;
2893 }
2894 else
2895 {
2896 g_clear_object (&context_win32->ipc_window);
2897 return FALSE;
2898 }
2899 }
2900
2901 static void
2902 gdk_win32_drag_context_cancel (GdkDragContext *context,
2903 GdkDragCancelReason reason)
2904 {
2905 const gchar *reason_str = NULL;
2906 switch (reason)
2907 {
2908 case GDK_DRAG_CANCEL_NO_TARGET:
2909 reason_str = "no target";
2910 break;
2911 case GDK_DRAG_CANCEL_USER_CANCELLED:
2912 reason_str = "user cancelled";
2913 break;
2914 case GDK_DRAG_CANCEL_ERROR:
2915 reason_str = "error";
2916 break;
2917 default:
2918 reason_str = "<unknown>";
2919 break;
2920 }
2921
2922 GDK_NOTE (DND, g_print ("gdk_drag_context_cancel: 0x%p %s\n",
2923 context,
2924 reason_str));
2925 drag_context_ungrab (context);
2926 gdk_drag_drop_done (context, FALSE);
2927 }
2928
2929 static void
2930 gdk_win32_drag_context_drop_performed (GdkDragContext *context,
2931 guint32 time_)
2932 {
2933 GDK_NOTE (DND, g_print ("gdk_drag_context_drop_performed: 0x%p %u\n",
2934 context,
2935 time_));
2936 gdk_drag_drop (context, time_);
2937 drag_context_ungrab (context);
2938 }
2939
2940 #define BIG_STEP 20
2941 #define SMALL_STEP 1
2942
2943 static void
2944 gdk_drag_get_current_actions (GdkModifierType state,
2945 gint button,
2946 GdkDragAction actions,
2947 GdkDragAction *suggested_action,
2948 GdkDragAction *possible_actions)
2949 {
2950 *suggested_action = 0;
2951 *possible_actions = 0;
2952
2953 if ((button == GDK_BUTTON_MIDDLE || button == GDK_BUTTON_SECONDARY) && (actions & GDK_ACTION_ASK))
2954 {
2955 *suggested_action = GDK_ACTION_ASK;
2956 *possible_actions = actions;
2957 }
2958 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
2959 {
2960 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
2961 {
2962 if (actions & GDK_ACTION_LINK)
2963 {
2964 *suggested_action = GDK_ACTION_LINK;
2965 *possible_actions = GDK_ACTION_LINK;
2966 }
2967 }
2968 else if (state & GDK_CONTROL_MASK)
2969 {
2970 if (actions & GDK_ACTION_COPY)
2971 {
2972 *suggested_action = GDK_ACTION_COPY;
2973 *possible_actions = GDK_ACTION_COPY;
2974 }
2975 }
2976 else
2977 {
2978 if (actions & GDK_ACTION_MOVE)
2979 {
2980 *suggested_action = GDK_ACTION_MOVE;
2981 *possible_actions = GDK_ACTION_MOVE;
2982 }
2983 }
2984 }
2985 else
2986 {
2987 *possible_actions = actions;
2988
2989 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
2990 *suggested_action = GDK_ACTION_ASK;
2991 else if (actions & GDK_ACTION_COPY)
2992 *suggested_action = GDK_ACTION_COPY;
2993 else if (actions & GDK_ACTION_MOVE)
2994 *suggested_action = GDK_ACTION_MOVE;
2995 else if (actions & GDK_ACTION_LINK)
2996 *suggested_action = GDK_ACTION_LINK;
2997 }
2998 }
2999
3000 static void
3001 gdk_drag_update (GdkDragContext *context,
3002 gdouble x_root,
3003 gdouble y_root,
3004 GdkModifierType mods,
3005 guint32 evtime)
3006 {
3007 GdkWin32DragContext *win32_context = GDK_WIN32_DRAG_CONTEXT (context);
3008 GdkDragAction action, possible_actions;
3009 GdkWindow *dest_window;
3010 GdkDragProtocol protocol;
3011
3012 gdk_drag_get_current_actions (mods, GDK_BUTTON_PRIMARY, win32_context->actions,
3013 &action, &possible_actions);
3014
3015 gdk_drag_find_window_for_screen (context,
3016 win32_context->drag_window,
3017 gdk_display_get_default_screen (gdk_display_get_default ()),
3018 x_root, y_root, &dest_window, &protocol);
3019
3020 gdk_drag_motion (context, dest_window, protocol, x_root, y_root,
3021 action, possible_actions, evtime);
3022 }
3023
3024 static gboolean
3025 gdk_dnd_handle_motion_event (GdkDragContext *context,
3026 const GdkEventMotion *event)
3027 {
3028 GdkModifierType state;
3029
3030 if (!gdk_event_get_state ((GdkEvent *) event, &state))
3031 return FALSE;
3032
3033 GDK_NOTE (DND, g_print ("gd_dnd_handle_motion_event: 0x%p\n",
3034 context));
3035
3036 gdk_drag_update (context, event->x_root, event->y_root, state,
3037 gdk_event_get_time ((GdkEvent *) event));
3038 return TRUE;
3039 }
3040
3041 static gboolean
3042 gdk_dnd_handle_key_event (GdkDragContext *context,
3043 const GdkEventKey *event)
3044 {
3045 GdkWin32DragContext *win32_context = GDK_WIN32_DRAG_CONTEXT (context);
3046 GdkModifierType state;
3047 GdkWindow *root_window;
3048 GdkDevice *pointer;
3049 gint dx, dy;
3050
3051 GDK_NOTE (DND, g_print ("gdk_dnd_handle_key_event: 0x%p\n",
3052 context));
3053
3054 dx = dy = 0;
3055 state = event->state;
3056 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
3057
3058 if (event->type == GDK_KEY_PRESS)
3059 {
3060 switch (event->keyval)
3061 {
3062 case GDK_KEY_Escape:
3063 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_USER_CANCELLED);
3064 return TRUE;
3065
3066 case GDK_KEY_space:
3067 case GDK_KEY_Return:
3068 case GDK_KEY_ISO_Enter:
3069 case GDK_KEY_KP_Enter:
3070 case GDK_KEY_KP_Space:
3071 if ((gdk_drag_context_get_selected_action (context) != 0) &&
3072 (gdk_drag_context_get_dest_window (context) != NULL))
3073 {
3074 g_signal_emit_by_name (context, "drop-performed",
3075 gdk_event_get_time ((GdkEvent *) event));
3076 }
3077 else
3078 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_NO_TARGET);
3079
3080 return TRUE;
3081
3082 case GDK_KEY_Up:
3083 case GDK_KEY_KP_Up:
3084 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
3085 break;
3086
3087 case GDK_KEY_Down:
3088 case GDK_KEY_KP_Down:
3089 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
3090 break;
3091
3092 case GDK_KEY_Left:
3093 case GDK_KEY_KP_Left:
3094 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
3095 break;
3096
3097 case GDK_KEY_Right:
3098 case GDK_KEY_KP_Right:
3099 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
3100 break;
3101 }
3102 }
3103
3104 /* The state is not yet updated in the event, so we need
3105 * to query it here.
3106 */
3107 root_window = gdk_screen_get_root_window (gdk_window_get_screen (win32_context->ipc_window));
3108 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
3109
3110 if (dx != 0 || dy != 0)
3111 {
3112 win32_context->last_x += dx;
3113 win32_context->last_y += dy;
3114 gdk_device_warp (pointer,
3115 gdk_window_get_screen (win32_context->ipc_window),
3116 win32_context->last_x, win32_context->last_y);
3117 }
3118
3119 gdk_drag_update (context, win32_context->last_x, win32_context->last_y, state,
3120 gdk_event_get_time ((GdkEvent *) event));
3121
3122 return TRUE;
3123 }
3124
3125 static gboolean
3126 gdk_dnd_handle_grab_broken_event (GdkDragContext *context,
3127 const GdkEventGrabBroken *event)
3128 {
3129 GdkWin32DragContext *win32_context = GDK_WIN32_DRAG_CONTEXT (context);
3130
3131 GDK_NOTE (DND, g_print ("gdk_dnd_handle_grab_broken_event: 0x%p\n",
3132 context));
3133
3134 /* Don't cancel if we break the implicit grab from the initial button_press.
3135 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
3136 * example, when changing the drag cursor.
3137 */
3138 if (event->implicit ||
3139 event->grab_window == win32_context->drag_window ||
3140 event->grab_window == win32_context->ipc_window)
3141 return FALSE;
3142
3143 if (gdk_event_get_device ((GdkEvent *) event) !=
3144 gdk_drag_context_get_device (context))
3145 return FALSE;
3146
3147 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_ERROR);
3148 return TRUE;
3149 }
3150
3151 static gboolean
3152 gdk_dnd_handle_button_event (GdkDragContext *context,
3153 const GdkEventButton *event)
3154 {
3155 GDK_NOTE (DND, g_print ("gdk_dnd_handle_button_event: 0x%p\n",
3156 context));
3157
3158 #if 0
3159 /* FIXME: Check the button matches */
3160 if (event->button != win32_context->button)
3161 return FALSE;
3162 #endif
3163
3164 if ((gdk_drag_context_get_selected_action (context) != 0) &&
3165 (gdk_drag_context_get_dest_window (context) != NULL))
3166 {
3167 g_signal_emit_by_name (context, "drop-performed",
3168 gdk_event_get_time ((GdkEvent *) event));
3169 }
3170 else
3171 gdk_drag_context_cancel (context, GDK_DRAG_CANCEL_NO_TARGET);
3172
3173 return TRUE;
3174 }
3175
3176 gboolean
3177 gdk_dnd_handle_drag_status (GdkDragContext *context,
3178 const GdkEventDND *event)
3179 {
3180 GdkWin32DragContext *context_win32 = GDK_WIN32_DRAG_CONTEXT (context);
3181 GdkDragAction action;
3182
3183 GDK_NOTE (DND, g_print ("gdk_dnd_handle_drag_status: 0x%p\n",
3184 context));
3185
3186 if (context != event->context)
3187 return FALSE;
3188
3189 action = gdk_drag_context_get_selected_action (context);
3190
3191 if (action != context_win32->current_action)
3192 {
3193 context_win32->current_action = action;
3194 g_signal_emit_by_name (context, "action-changed", action);
3195 }
3196
3197 return TRUE;
3198 }
3199
3200 static gboolean
3201 gdk_dnd_handle_drop_finished (GdkDragContext *context,
3202 const GdkEventDND *event)
3203 {
3204 GdkWin32DragContext *win32_context = GDK_WIN32_DRAG_CONTEXT (context);
3205
3206 GDK_NOTE (DND, g_print ("gdk_dnd_handle_drop_finihsed: 0x%p\n",
3207 context));
3208
3209 if (context != event->context)
3210 return FALSE;
3211
3212 g_signal_emit_by_name (context, "dnd-finished");
3213 gdk_drag_drop_done (context, !win32_context->drop_failed);
3214 gdk_win32_selection_clear_targets (gdk_display_get_default (),
3215 gdk_win32_drag_context_get_selection (context));
3216
3217 return TRUE;
3218 }
3219
3220 gboolean
3221 gdk_win32_drag_context_handle_event (GdkDragContext *context,
3222 const GdkEvent *event)
3223 {
3224 GdkWin32DragContext *win32_context = GDK_WIN32_DRAG_CONTEXT (context);
3225
3226 if (!context->is_source)
3227 return FALSE;
3228 if (!win32_context->grab_seat && event->type != GDK_DROP_FINISHED)
3229 return FALSE;
3230
3231 switch (event->type)
3232 {
3233 case GDK_MOTION_NOTIFY:
3234 return gdk_dnd_handle_motion_event (context, &event->motion);
3235 case GDK_BUTTON_RELEASE:
3236 return gdk_dnd_handle_button_event (context, &event->button);
3237 case GDK_KEY_PRESS:
3238 case GDK_KEY_RELEASE:
3239 return gdk_dnd_handle_key_event (context, &event->key);
3240 case GDK_GRAB_BROKEN:
3241 return gdk_dnd_handle_grab_broken_event (context, &event->grab_broken);
3242 case GDK_DRAG_STATUS:
3243 return gdk_dnd_handle_drag_status (context, &event->dnd);
3244 case GDK_DROP_FINISHED:
3245 return gdk_dnd_handle_drop_finished (context, &event->dnd);
3246 default:
3247 break;
3248 }
3249
3250 return FALSE;
3251 }
3252
3253 void
3254 gdk_win32_drag_context_action_changed (GdkDragContext *context,
3255 GdkDragAction action)
3256 {
3257 GdkCursor *cursor;
3258
3259 cursor = gdk_drag_get_cursor (context, action);
3260 gdk_drag_context_set_cursor (context, cursor);
3261 }
3262
3263 static GdkWindow *
3264 gdk_win32_drag_context_get_drag_window (GdkDragContext *context)
3265 {
3266 return GDK_WIN32_DRAG_CONTEXT (context)->drag_window;
3267 }
3268
3269 static void
3270 gdk_win32_drag_context_set_hotspot (GdkDragContext *context,
3271 gint hot_x,
3272 gint hot_y)
3273 {
3274 GdkWin32DragContext *win32_context = GDK_WIN32_DRAG_CONTEXT (context);
3275
3276 GDK_NOTE (DND, g_print ("gdk_drag_context_set_hotspot: 0x%p %d:%d\n",
3277 context,
3278 hot_x, hot_y));
3279
3280 win32_context->hot_x = hot_x;
3281 win32_context->hot_y = hot_y;
3282
3283 if (win32_context->grab_seat)
3284 {
3285 /* DnD is managed, update current position */
3286 move_drag_window (context, win32_context->last_x, win32_context->last_y);
3287 }
3288 }
3289
3290 static void
3291 gdk_win32_drag_context_class_init (GdkWin32DragContextClass *klass)
3292 {
3293 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3294 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
3295
3296 object_class->finalize = gdk_win32_drag_context_finalize;
3297
3298 context_class->find_window = gdk_win32_drag_context_find_window;
3299 context_class->drag_status = gdk_win32_drag_context_drag_status;
3300 context_class->drag_motion = gdk_win32_drag_context_drag_motion;
3301 context_class->drag_abort = gdk_win32_drag_context_drag_abort;
3302 context_class->drag_drop = gdk_win32_drag_context_drag_drop;
3303 context_class->drop_reply = gdk_win32_drag_context_drop_reply;
3304 context_class->drop_finish = gdk_win32_drag_context_drop_finish;
3305 context_class->drop_status = gdk_win32_drag_context_drop_status;
3306 context_class->get_selection = gdk_win32_drag_context_get_selection;
3307
3308 context_class->get_drag_window = gdk_win32_drag_context_get_drag_window;
3309 context_class->set_hotspot = gdk_win32_drag_context_set_hotspot;
3310 context_class->drop_done = gdk_win32_drag_context_drop_done;
3311 context_class->manage_dnd = gdk_win32_drag_context_manage_dnd;
3312 context_class->set_cursor = gdk_win32_drag_context_set_cursor;
3313 context_class->cancel = gdk_win32_drag_context_cancel;
3314 context_class->drop_performed = gdk_win32_drag_context_drop_performed;
3315 context_class->handle_event = gdk_win32_drag_context_handle_event;
3316 context_class->action_changed = gdk_win32_drag_context_action_changed;
3317
3318 }