Debian Codegraph @

gdk/win32/gdkdrop-win32.c

raw symbols SCIP
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 /* The mingw.org compiler does not export GUIDS in it's import library. To work
34 * around that, define INITGUID to have the GUIDS declared. */
35 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
36 #define INITGUID
37 #endif
38
39 #include "gdkdropprivate.h"
40
41 #include "gdkdrag.h"
42 #include "gdkprivate-win32.h"
43 #include "gdkwin32.h"
44 #include "gdkwin32dnd.h"
45 #include "gdkdisplayprivate.h"
46 #include "gdkwin32dnd-private.h"
47 #include "gdkdisplay-win32.h"
48 #include "gdkdeviceprivate.h"
49 #include "gdkhdataoutputstream-win32.h"
50
51 #include <ole2.h>
52
53 #include <shlobj.h>
54 #include <shlguid.h>
55 #include <objidl.h>
56 #include <glib/gi18n-lib.h>
57
58 #include <gdk/gdk.h>
59 #include <glib/gstdio.h>
60
61 #define GDK_TYPE_WIN32_DROP (gdk_win32_drop_get_type ())
62 #define GDK_WIN32_DROP(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_WIN32_DROP, GdkWin32Drop))
63 #define GDK_WIN32_DROP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_WIN32_DROP, GdkWin32DropClass))
64 #define GDK_IS_WIN32_DROP(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_WIN32_DROP))
65 #define GDK_IS_WIN32_DROP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_WIN32_DROP))
66 #define GDK_WIN32_DROP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_WIN32_DROP, GdkWin32DropClass))
67
68 typedef struct _GdkWin32Drop GdkWin32Drop;
69 typedef struct _GdkWin32DropClass GdkWin32DropClass;
70
71 struct _GdkWin32Drop
72 {
73 GdkDrop drop;
74
75 /* The drag protocol being in use */
76 GdkDragProtocol protocol;
77
78 /* The actions supported at GTK level. Set in gdk_win32_drop_status(). */
79 GdkDragAction actions;
80
81 guint scale; /* Temporarily caches the HiDPI scale */
82 int last_x; /* Coordinates from last event, in GDK space */
83 int last_y;
84 DWORD last_key_state; /* Key state from last event */
85
86 /* Just like GdkDrop->formats, but an array, and with format IDs
87 * stored inside.
88 */
89 GArray *droptarget_w32format_contentformat_map;
90
91 /* The list from WM_DROPFILES is store here temporarily,
92 * until the next gdk_win32_drop_read_async ()
93 */
94 char *dropfiles_list;
95
96 guint drop_finished : 1; /* FALSE until gdk_drop_finish() is called */
97 guint drop_failed : 1; /* Whether the drop was unsuccessful */
98 };
99
100 struct _GdkWin32DropClass
101 {
102 GdkDropClass parent_class;
103 };
104
105 GType gdk_win32_drop_get_type (void);
106
107 G_DEFINE_TYPE (GdkWin32Drop, gdk_win32_drop, GDK_TYPE_DROP)
108
109 /* This structure is presented to COM as an object that
110 * implements IDropTarget interface. Every surface that
111 * can be a droptarget has one of these.
112 */
113 struct _drop_target_context
114 {
115 IDropTarget idt;
116 int ref_count;
117
118 /* The drop object we create when a drag enters our surface.
119 * The drop object is destroyed when the drag leaves.
120 */
121 GdkDrop *drop;
122 /* We get this at the object at creation time and keep
123 * it indefinitely. Drops (see above) come and go, but
124 * this surface remains the same.
125 * This is not a reference, as drop_target_context must not
126 * outlive the surface it's attached to.
127 * drop_target_context is not folded into GdkWin32Surface
128 * only because it's easier to present it to COM as a separate
129 * object when it's allocated separately.
130 */
131 GdkSurface *surface;
132 /* This is given to us by the OS, we store it here
133 * until the drag leaves our surface HWND. It is referenced
134 * (using COM reference counting).
135 */
136 IDataObject *data_object;
137 };
138
139 static void
140 gdk_win32_drop_init (GdkWin32Drop *drop)
141 {
142 GDK_NOTE (DND, g_print ("gdk_win32_drop_init %p\n", drop));
143 }
144
145 static void
146 gdk_win32_drop_finalize (GObject *object)
147 {
148 GdkDrop *drop;
149 GdkWin32Drop *drop_win32;
150
151 GDK_NOTE (DND, g_print ("gdk_win32_drop_finalize %p\n", object));
152
153 drop = GDK_DROP (object);
154
155 drop_win32 = GDK_WIN32_DROP (drop);
156
157 g_array_unref (drop_win32->droptarget_w32format_contentformat_map);
158
159 G_OBJECT_CLASS (gdk_win32_drop_parent_class)->finalize (object);
160 }
161
162 static GdkDrop *
163 gdk_drop_new (GdkDisplay *display,
164 GdkDevice *device,
165 GdkDrag *drag,
166 GdkContentFormats *formats,
167 GdkSurface *surface,
168 GdkDragProtocol protocol)
169 {
170 GdkWin32Drop *drop_win32;
171 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
172
173 drop_win32 = g_object_new (GDK_TYPE_WIN32_DROP,
174 "device", device,
175 "drag", drag,
176 "formats", formats,
177 "surface", surface,
178 NULL);
179
180 drop_win32->scale = gdk_win32_display_get_monitor_scale_factor (display_win32, NULL, NULL);
181 drop_win32->protocol = protocol;
182
183 return GDK_DROP (drop_win32);
184 }
185
186
187 #define PRINT_GUID(guid) \
188 g_print ("%.08lx-%.04x-%.04x-%.02x%.02x-%.02x%.02x%.02x%.02x%.02x%.02x", \
189 ((gulong *) guid)[0], \
190 ((gushort *) guid)[2], \
191 ((gushort *) guid)[3], \
192 ((guchar *) guid)[8], \
193 ((guchar *) guid)[9], \
194 ((guchar *) guid)[10], \
195 ((guchar *) guid)[11], \
196 ((guchar *) guid)[12], \
197 ((guchar *) guid)[13], \
198 ((guchar *) guid)[14], \
199 ((guchar *) guid)[15]);
200
201 static ULONG STDMETHODCALLTYPE
202 idroptarget_addref (LPDROPTARGET This)
203 {
204 drop_target_context *ctx = (drop_target_context *) This;
205
206 int ref_count = ++ctx->ref_count;
207
208 GDK_NOTE (DND, g_print ("idroptarget_addref %p %d\n", This, ref_count));
209
210 return ref_count;
211 }
212
213 static HRESULT STDMETHODCALLTYPE
214 idroptarget_queryinterface (LPDROPTARGET This,
215 REFIID riid,
216 LPVOID *ppvObject)
217 {
218 GDK_NOTE (DND, {
219 g_print ("idroptarget_queryinterface %p ", This);
220 PRINT_GUID (riid);
221 });
222
223 *ppvObject = NULL;
224
225 if (IsEqualGUID (riid, &IID_IUnknown))
226 {
227 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
228 idroptarget_addref (This);
229 *ppvObject = This;
230 return S_OK;
231 }
232 else if (IsEqualGUID (riid, &IID_IDropTarget))
233 {
234 GDK_NOTE (DND, g_print ("...IDropTarget S_OK\n"));
235 idroptarget_addref (This);
236 *ppvObject = This;
237 return S_OK;
238 }
239 else
240 {
241 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
242 return E_NOINTERFACE;
243 }
244 }
245
246 static ULONG STDMETHODCALLTYPE
247 idroptarget_release (LPDROPTARGET This)
248 {
249 drop_target_context *ctx = (drop_target_context *) This;
250
251 int ref_count = --ctx->ref_count;
252
253 GDK_NOTE (DND, g_print ("idroptarget_release %p %d\n", This, ref_count));
254
255 if (ref_count == 0)
256 {
257 g_clear_object (&ctx->drop);
258 g_free (This);
259 }
260
261 return ref_count;
262 }
263
264 static GdkContentFormats *
265 query_object_formats (GdkDisplay *display,
266 LPDATAOBJECT pDataObj,
267 GArray *w32format_contentformat_map)
268 {
269 IEnumFORMATETC *pfmt = NULL;
270 FORMATETC fmt;
271 HRESULT hr;
272 GdkContentFormatsBuilder *builder;
273 GdkContentFormats *result_formats;
274
275 hr = IDataObject_EnumFormatEtc (pDataObj, DATADIR_GET, &pfmt);
276 if (FAILED (hr))
277 return gdk_content_formats_new (NULL, 0);
278
279 builder = gdk_content_formats_builder_new ();
280 hr = IEnumFORMATETC_Next (pfmt, 1, &fmt, NULL);
281
282 while (SUCCEEDED (hr) && hr != S_FALSE)
283 {
284 gboolean is_predef;
285 char *registered_name = _gdk_win32_get_clipboard_format_name (fmt.cfFormat, &is_predef);
286
287 if (registered_name && is_predef)
288 GDK_NOTE (DND, g_print ("supported built-in source format 0x%x %s\n", fmt.cfFormat, registered_name));
289 else if (registered_name)
290 GDK_NOTE (DND, g_print ("supported source format 0x%x %s\n", fmt.cfFormat, registered_name));
291 else
292 GDK_NOTE (DND, g_print ("supported unnamed? source format 0x%x\n", fmt.cfFormat));
293
294 g_free (registered_name);
295 gdk_win32_clipdrop_add_win32_format_to_pairs (gdk_win32_display_get_clipdrop (display), fmt.cfFormat, w32format_contentformat_map, builder);
296 hr = IEnumFORMATETC_Next (pfmt, 1, &fmt, NULL);
297 }
298
299 gdk_win32_com_clear (&pfmt);
300
301 result_formats = gdk_content_formats_builder_free_to_formats (builder);
302
303 return result_formats;
304 }
305
306 static void
307 set_data_object (LPDATAOBJECT *location, LPDATAOBJECT data_object)
308 {
309 gdk_win32_com_clear (location);
310
311 *location = data_object;
312
313 if (*location != NULL)
314 IDataObject_AddRef (*location);
315 }
316
317 /* Figures out an action that the user forces onto us by
318 * pressing some modifier keys.
319 */
320 static GdkDragAction
321 get_user_action (DWORD grfKeyState)
322 {
323 /* Windows explorer does this:
324 * 'C'ontrol for 'C'opy
325 * a'L't (or Contro'L' + Shift) for 'L'ink
326 * Shift for Move
327 * Control + Alt or Shift + Alt or Control + Alt + Shift for Default action (see below).
328 *
329 * Default action is 'Copy' when dragging between drives, 'Move' otherwise.
330 * For GTK 'between drives' turns into 'between applications'.
331 */
332 if (((grfKeyState & (MK_CONTROL | MK_ALT)) == (MK_CONTROL | MK_ALT)) ||
333 ((grfKeyState & (MK_ALT | MK_SHIFT)) == (MK_ALT | MK_SHIFT)) ||
334 ((grfKeyState & (MK_CONTROL | MK_ALT | MK_SHIFT)) == (MK_CONTROL | MK_ALT | MK_SHIFT)))
335 {
336 return 0;
337 }
338 else if ((grfKeyState & (MK_CONTROL | MK_SHIFT)) == (MK_CONTROL | MK_SHIFT))
339 return GDK_ACTION_LINK;
340 else if (grfKeyState & MK_CONTROL)
341 return GDK_ACTION_COPY;
342 else if (grfKeyState & MK_ALT)
343 return GDK_ACTION_MOVE;
344
345 return 0;
346 }
347
348 static DWORD
349 drop_effect_for_actions (GdkDragAction actions)
350 {
351 DWORD effects = 0;
352 int effect_count = 0;
353
354 if (actions & GDK_ACTION_MOVE)
355 {
356 effects |= DROPEFFECT_MOVE;
357 effect_count++;
358 }
359 if (actions & GDK_ACTION_LINK)
360 {
361 effects |= DROPEFFECT_LINK;
362 effect_count++;
363 }
364 if (actions & GDK_ACTION_COPY)
365 {
366 effects |= DROPEFFECT_COPY;
367 effect_count++;
368 }
369
370 if (effect_count == 0)
371 effects = DROPEFFECT_NONE;
372 else if (effect_count > 1)
373 /* Actually it should be DROPEFECT_ASK, but Windows doesn't support that */
374 effects = DROPEFFECT_COPY;
375
376 return effects;
377 }
378
379 static GdkDragAction
380 actions_for_drop_effects (DWORD effects)
381 {
382 GdkDragAction actions = 0;
383
384 if (effects & DROPEFFECT_MOVE)
385 actions |= GDK_ACTION_MOVE;
386 if (effects & DROPEFFECT_LINK)
387 actions |= GDK_ACTION_LINK;
388 if (effects & DROPEFFECT_COPY)
389 actions |= GDK_ACTION_COPY;
390
391 return actions;
392 }
393
394 static GdkDragAction
395 filter_actions (GdkDragAction actions,
396 GdkDragAction filter)
397 {
398 return actions & filter;
399 }
400
401 static GdkDragAction
402 set_source_actions_helper (GdkDrop *drop,
403 GdkDragAction actions,
404 DWORD grfKeyState)
405 {
406 GdkDragAction user_action;
407 GdkWin32Drop *drop_win32;
408
409 user_action = get_user_action (grfKeyState);
410
411 drop_win32 = GDK_WIN32_DROP (drop);
412 drop_win32->actions = actions;
413
414 if (user_action != 0)
415 gdk_drop_set_actions (drop, user_action);
416 else
417 gdk_drop_set_actions (drop, actions);
418
419 return actions;
420 }
421
422 /* Utility function to translate screen coordinates to surface-relative
423 * coordinates. This routine only works with pixel values that aren't
424 * scaled by any GDK DPI scale factor.
425 */
426 static void
427 unscaled_screen_to_client (GdkSurface* surface,
428 double screen_x,
429 double screen_y,
430 double *client_x,
431 double *client_y)
432 {
433 POINT client_origin;
434
435 client_origin.x = 0;
436 client_origin.y = 0;
437 ClientToScreen (GDK_SURFACE_HWND (surface), &client_origin);
438 *client_x = screen_x - client_origin.x;
439 *client_y = screen_y - client_origin.y;
440 }
441
442 /* The pdwEffect here initially points
443 * to a DWORD that contains the value of dwOKEffects argument in DoDragDrop,
444 * i.e. the drag action that the drag source deems acceptable.
445 * On return it should point to the effect value that denotes the
446 * action that is going to happen on drop, and that is what DoDragDrop will
447 * put into the DWORD that pdwEffect was pointing to.
448 */
449 static HRESULT STDMETHODCALLTYPE
450 idroptarget_dragenter (LPDROPTARGET This,
451 LPDATAOBJECT pDataObj,
452 DWORD grfKeyState,
453 POINTL pt,
454 LPDWORD pdwEffect_and_dwOKEffects)
455 {
456 drop_target_context *ctx = (drop_target_context *) This;
457 GdkDrop *drop;
458 GdkWin32Drop *drop_win32;
459 GdkDisplay *display;
460 int pt_x;
461 int pt_y;
462 double x = 0.0;
463 double y = 0.0;
464 GdkDrag *drag;
465 GdkDragAction source_actions;
466 GdkDragAction dest_actions;
467 GdkContentFormats *formats;
468 GArray *droptarget_w32format_contentformat_map;
469
470 GDK_NOTE (DND, g_print ("idroptarget_dragenter %p @ %ld : %ld"
471 " for dest surface 0x%p"
472 ". dwOKEffects = %lu\n",
473 This, pt.x, pt.y,
474 ctx->surface,
475 *pdwEffect_and_dwOKEffects));
476
477 g_clear_object (&ctx->drop);
478
479 /* Try to find the GdkDrag object for this DnD operation,
480 * if it originated in our own application.
481 */
482 drag = NULL;
483
484 if (ctx->surface)
485 drag = gdk_win32_find_drag_for_dest_surface (ctx->surface);
486
487 display = gdk_surface_get_display (ctx->surface);
488
489 droptarget_w32format_contentformat_map = g_array_new (FALSE, FALSE, sizeof (GdkWin32ContentFormatPair));
490 formats = query_object_formats (display, pDataObj, droptarget_w32format_contentformat_map);
491 drop = gdk_drop_new (display,
492 gdk_seat_get_pointer (gdk_display_get_default_seat (display)),
493 drag,
494 formats,
495 ctx->surface,
496 GDK_DRAG_PROTO_OLE2);
497 drop_win32 = GDK_WIN32_DROP (drop);
498 drop_win32->droptarget_w32format_contentformat_map = droptarget_w32format_contentformat_map;
499 gdk_content_formats_unref (formats);
500
501 ctx->drop = drop;
502
503 source_actions = set_source_actions_helper (drop,
504 actions_for_drop_effects (*pdwEffect_and_dwOKEffects),
505 grfKeyState);
506
507 set_data_object (&ctx->data_object, pDataObj);
508 pt_x = pt.x / drop_win32->scale;
509 pt_y = pt.y / drop_win32->scale;
510
511 unscaled_screen_to_client (ctx->surface, pt.x, pt.y, &x, &y);
512 x /= drop_win32->scale;
513 y /= drop_win32->scale;
514
515 gdk_drop_emit_enter_event (drop, TRUE, x, y, GDK_CURRENT_TIME);
516 drop_win32->last_key_state = grfKeyState;
517 drop_win32->last_x = pt_x;
518 drop_win32->last_y = pt_y;
519 dest_actions = filter_actions (gdk_drop_get_actions (drop), source_actions);
520 *pdwEffect_and_dwOKEffects = drop_effect_for_actions (dest_actions);
521
522 GDK_NOTE (DND, g_print ("idroptarget_dragenter returns S_OK with actions %s"
523 " and drop effect %lu\n",
524 _gdk_win32_drag_action_to_string (dest_actions),
525 *pdwEffect_and_dwOKEffects));
526
527 return S_OK;
528 }
529
530 /* NOTE: This method is called continuously, even if nothing is
531 * happening, as long as the drag operation is in progress and
532 * the cursor is above our surface.
533 * It is OK to return a "safe" dropeffect value (DROPEFFECT_NONE,
534 * to indicate that the drop is not possible here), when we
535 * do not yet have any real information about acceptability of
536 * the drag, because we will have another opportunity to return
537 * the "right" value (once we know what it is, after GTK processes
538 * the events we emit) very soon.
539 */
540 static HRESULT STDMETHODCALLTYPE
541 idroptarget_dragover (LPDROPTARGET This,
542 DWORD grfKeyState,
543 POINTL pt,
544 LPDWORD pdwEffect_and_dwOKEffects)
545 {
546 drop_target_context *ctx = (drop_target_context *) This;
547 GdkWin32Drop *drop_win32 = GDK_WIN32_DROP (ctx->drop);
548 int pt_x = pt.x / drop_win32->scale;
549 int pt_y = pt.y / drop_win32->scale;
550 GdkDragAction source_actions;
551 GdkDragAction dest_actions;
552
553 source_actions = actions_for_drop_effects (*pdwEffect_and_dwOKEffects);
554
555 GDK_NOTE (DND, g_print ("idroptarget_dragover %p @ %d : %d"
556 " (raw %ld : %ld)"
557 ", dwOKEffects = %lu"
558 ", suggests %d action\n",
559 This, pt_x, pt_y,
560 pt.x, pt.y,
561 *pdwEffect_and_dwOKEffects,
562 source_actions));
563
564 if (pt_x != drop_win32->last_x ||
565 pt_y != drop_win32->last_y ||
566 grfKeyState != drop_win32->last_key_state ||
567 source_actions != drop_win32->actions)
568 {
569 double x = 0.0;
570 double y = 0.0;
571
572 unscaled_screen_to_client (ctx->surface, pt.x, pt.y, &x, &y);
573 x /= drop_win32->scale;
574 y /= drop_win32->scale;
575
576 set_source_actions_helper (ctx->drop, source_actions, grfKeyState);
577
578 gdk_drop_emit_motion_event (ctx->drop, TRUE, x, y, GDK_CURRENT_TIME);
579 drop_win32->last_key_state = grfKeyState;
580 drop_win32->last_x = pt_x;
581 drop_win32->last_y = pt_y;
582 }
583
584 dest_actions = filter_actions (gdk_drop_get_actions (ctx->drop), source_actions);
585 *pdwEffect_and_dwOKEffects = drop_effect_for_actions (dest_actions);
586
587 GDK_NOTE (DND, g_print ("idroptarget_dragover returns S_OK with actions %s"
588 " and effect %lu\n",
589 _gdk_win32_drag_action_to_string (dest_actions),
590 *pdwEffect_and_dwOKEffects));
591
592 return S_OK;
593 }
594
595 static HRESULT STDMETHODCALLTYPE
596 idroptarget_dragleave (LPDROPTARGET This)
597 {
598 drop_target_context *ctx = (drop_target_context *) This;
599
600 GDK_NOTE (DND, g_print ("idroptarget_dragleave %p S_OK\n", This));
601
602 gdk_drop_emit_leave_event (GDK_DROP (ctx->drop), TRUE, GDK_CURRENT_TIME);
603
604 g_clear_object (&ctx->drop);
605 set_data_object (&ctx->data_object, NULL);
606
607 return S_OK;
608 }
609
610 static HRESULT STDMETHODCALLTYPE
611 idroptarget_drop (LPDROPTARGET This,
612 LPDATAOBJECT pDataObj,
613 DWORD grfKeyState,
614 POINTL pt,
615 LPDWORD pdwEffect_and_dwOKEffects)
616 {
617 drop_target_context *ctx = (drop_target_context *) This;
618 GdkWin32Drop *drop_win32 = GDK_WIN32_DROP (ctx->drop);
619 double x = 0.0;
620 double y = 0.0;
621 GdkDragAction dest_action;
622
623 GDK_NOTE (DND, g_print ("idroptarget_drop %p ", This));
624
625 if (pDataObj == NULL)
626 {
627 GDK_NOTE (DND, g_print ("E_POINTER\n"));
628 gdk_drop_emit_leave_event (ctx->drop, TRUE, GDK_CURRENT_TIME);
629 g_clear_object (&ctx->drop);
630 set_data_object (&ctx->data_object, NULL);
631
632 return E_POINTER;
633 }
634
635 set_source_actions_helper (ctx->drop,
636 actions_for_drop_effects (*pdwEffect_and_dwOKEffects),
637 grfKeyState);
638
639 drop_win32->drop_finished = FALSE;
640
641 unscaled_screen_to_client (ctx->surface, pt.x, pt.y, &x, &y);
642 x /= drop_win32->scale;
643 y /= drop_win32->scale;
644
645 gdk_drop_emit_motion_event (ctx->drop, TRUE, x, y, GDK_CURRENT_TIME);
646
647 gdk_drop_emit_drop_event (ctx->drop, TRUE, x, y, GDK_CURRENT_TIME);
648
649 gdk_drop_emit_leave_event (ctx->drop, TRUE, GDK_CURRENT_TIME);
650
651 while (!drop_win32->drop_finished)
652 g_main_context_iteration (NULL, FALSE);
653
654 /* Notify OLE of the DnD result
655 * Special case:
656 * drop_win32->actions is guaranteed to contain 1 action after gdk_drop_finish ()
657 */
658 dest_action = drop_win32->actions;
659 *pdwEffect_and_dwOKEffects = drop_effect_for_actions (dest_action);
660
661 g_clear_object (&ctx->drop);
662 set_data_object (&ctx->data_object, NULL);
663
664 GDK_NOTE (DND, g_print ("drop S_OK with effect %lx\n", *pdwEffect_and_dwOKEffects));
665
666 return S_OK;
667 }
668
669 static IDropTargetVtbl idt_vtbl = {
670 idroptarget_queryinterface,
671 idroptarget_addref,
672 idroptarget_release,
673 idroptarget_dragenter,
674 idroptarget_dragover,
675 idroptarget_dragleave,
676 idroptarget_drop
677 };
678
679 static drop_target_context *
680 target_context_new (GdkSurface *surface)
681 {
682 drop_target_context *result;
683
684 result = g_new0 (drop_target_context, 1);
685 result->idt.lpVtbl = &idt_vtbl;
686 result->ref_count = 0;
687
688 result->surface = surface;
689
690 idroptarget_addref (&result->idt);
691
692 GDK_NOTE (DND, g_print ("target_context_new: %p (surface %p)\n", result, result->surface));
693
694 return result;
695 }
696
697 #if 0
698
699 /* From MS Knowledge Base article Q130698 */
700
701 static gboolean
702 resolve_link (HWND hWnd,
703 wchar_t *link,
704 char **lpszPath)
705 {
706 WIN32_FILE_ATTRIBUTE_DATA wfad;
707 HRESULT hr;
708 IShellLinkW *pslW = NULL;
709 IPersistFile *ppf = NULL;
710
711 /* Check if the file is empty first because IShellLink::Resolve for
712 * some reason succeeds with an empty file and returns an empty
713 * "link target". (#524151)
714 */
715 if (!GetFileAttributesExW (link, GetFileExInfoStandard, &wfad) ||
716 (wfad.nFileSizeHigh == 0 && wfad.nFileSizeLow == 0))
717 return FALSE;
718
719 /* Assume failure to start with: */
720 *lpszPath = 0;
721
722 /* Call CoCreateInstance to obtain the IShellLink interface
723 * pointer. This call fails if CoInitialize is not called, so it is
724 * assumed that CoInitialize has been called.
725 */
726
727 hr = CoCreateInstance (&CLSID_ShellLink,
728 NULL,
729 CLSCTX_INPROC_SERVER,
730 &IID_IShellLinkW,
731 (LPVOID *)&pslW);
732
733 if (SUCCEEDED (hr))
734 {
735 /* The IShellLink interface supports the IPersistFile
736 * interface. Get an interface pointer to it.
737 */
738 hr = pslW->lpVtbl->QueryInterface (pslW,
739 &IID_IPersistFile,
740 (LPVOID *) &ppf);
741 }
742
743 if (SUCCEEDED (hr))
744 {
745 /* Load the file. */
746 hr = ppf->lpVtbl->Load (ppf, link, STGM_READ);
747 }
748
749 if (SUCCEEDED (hr))
750 {
751 /* Resolve the link by calling the Resolve()
752 * interface function.
753 */
754 hr = pslW->lpVtbl->Resolve (pslW, hWnd, SLR_ANY_MATCH | SLR_NO_UI);
755 }
756
757 if (SUCCEEDED (hr))
758 {
759 wchar_t wtarget[MAX_PATH];
760
761 hr = pslW->lpVtbl->GetPath (pslW, wtarget, MAX_PATH, NULL, 0);
762 if (SUCCEEDED (hr))
763 *lpszPath = g_utf16_to_utf8 (wtarget, -1, NULL, NULL, NULL);
764 }
765
766 if (ppf)
767 ppf->lpVtbl->Release (ppf);
768
769 if (pslW)
770 pslW->lpVtbl->Release (pslW);
771
772 return SUCCEEDED (hr);
773 }
774
775 /* Check for filenames like C:\Users\tml\AppData\Local\Temp\d5qtkvvs.bmp */
776 static gboolean
777 filename_looks_tempish (const char *filename)
778 {
779 char *dirname;
780 char *p;
781 const char *q;
782 gboolean retval = FALSE;
783
784 dirname = g_path_get_dirname (filename);
785
786 p = dirname;
787 q = g_get_tmp_dir ();
788
789 while (*p && *q &&
790 ((G_IS_DIR_SEPARATOR (*p) && G_IS_DIR_SEPARATOR (*q)) ||
791 g_ascii_tolower (*p) == g_ascii_tolower (*q)))
792 p++, q++;
793
794 if (!*p && !*q)
795 retval = TRUE;
796
797 g_free (dirname);
798
799 return retval;
800 }
801
802 static gboolean
803 close_it (gpointer data)
804 {
805 close (GPOINTER_TO_INT (data));
806
807 return FALSE;
808 }
809
810 #endif
811
812 static void
813 gdk_win32_drop_status (GdkDrop *drop,
814 GdkDragAction actions,
815 GdkDragAction preferred)
816 {
817 GdkWin32Drop *drop_win32 = GDK_WIN32_DROP (drop);
818
819 g_return_if_fail (drop != NULL);
820
821 GDK_NOTE (DND, g_print ("gdk_win32_drop_status: %s\n"
822 " context=%p:{source_actions=%s, preferred=%s}\n",
823 _gdk_win32_drag_action_to_string (actions),
824 drop,
825 _gdk_win32_drag_action_to_string (gdk_drop_get_actions (drop)),
826 _gdk_win32_drag_action_to_string (preferred)));
827
828 if (preferred != 0)
829 actions = preferred;
830
831 gdk_drop_set_actions (drop, drop_win32->actions & actions);
832 }
833
834 static void
835 gdk_win32_drop_finish (GdkDrop *drop,
836 GdkDragAction action)
837 {
838 GdkWin32Drop *drop_win32 = GDK_WIN32_DROP (drop);
839
840 g_return_if_fail (drop != NULL);
841
842 GDK_NOTE (DND, g_print ("gdk_win32_drop_finish with action %s\n",
843 _gdk_win32_drag_action_to_string (action)));
844
845 drop_win32->actions = action;
846 drop_win32->drop_finished = TRUE;
847 }
848
849 #if 0
850
851 static GdkFilterReturn
852 gdk_destroy_filter (GdkXEvent *xev,
853 GdkEvent *event,
854 gpointer data)
855 {
856 MSG *msg = (MSG *) xev;
857
858 if (msg->message == WM_DESTROY)
859 {
860 IDropTarget *idtp = (IDropTarget *) data;
861
862 GDK_NOTE (DND, g_print ("gdk_destroy_filter: WM_DESTROY: %p\n", msg->hwnd));
863 #if 0
864 idtp->lpVtbl->Release (idtp);
865 #endif
866 RevokeDragDrop (msg->hwnd);
867 CoLockObjectExternal ((IUnknown*) idtp, FALSE, TRUE);
868 }
869 return GDK_FILTER_CONTINUE;
870 }
871
872 #endif
873
874 void
875 _gdk_win32_surface_register_dnd (GdkSurface *surface)
876 {
877 drop_target_context *ctx;
878 HRESULT hr;
879 GdkWin32Surface *impl;
880
881 g_return_if_fail (surface != NULL);
882
883 if (g_object_get_data (G_OBJECT (surface), "gdk-dnd-registered") != NULL)
884 return;
885 else
886 g_object_set_data (G_OBJECT (surface), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
887
888 GDK_NOTE (DND, g_print ("gdk_win32_surface_register_dnd: %p\n", GDK_SURFACE_HWND (surface)));
889
890 impl = GDK_WIN32_SURFACE (surface);
891
892 /* Return if surface is already setup for DND. */
893 if (impl->drop_target != NULL)
894 return;
895
896 ctx = target_context_new (surface);
897
898 hr = CoLockObjectExternal ((IUnknown *) &ctx->idt, TRUE, FALSE);
899 if (!SUCCEEDED (hr))
900 OTHER_API_FAILED ("CoLockObjectExternal");
901 else
902 {
903 hr = RegisterDragDrop (GDK_SURFACE_HWND (surface), &ctx->idt);
904 if (hr == DRAGDROP_E_ALREADYREGISTERED)
905 {
906 g_print ("DRAGDROP_E_ALREADYREGISTERED\n");
907 CoLockObjectExternal ((IUnknown *) &ctx->idt, FALSE, FALSE);
908 }
909 else if (!SUCCEEDED (hr))
910 OTHER_API_FAILED ("RegisterDragDrop");
911 else
912 {
913 impl->drop_target = ctx;
914 }
915 }
916 }
917
918 void
919 _gdk_win32_surface_unregister_dnd (GdkSurface *surface)
920 {
921 GdkWin32Surface *impl = GDK_WIN32_SURFACE (surface);
922
923 if (impl->drop_target)
924 idroptarget_release (&impl->drop_target->idt);
925 }
926
927 static gpointer
928 grab_data_from_hdata (GTask *task,
929 HANDLE hdata,
930 gsize *data_len)
931 {
932 gpointer ptr;
933 SIZE_T length;
934 guchar *data;
935
936 ptr = GlobalLock (hdata);
937 if (ptr == NULL)
938 {
939 DWORD error_code = GetLastError ();
940 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
941 _("Cannot get DnD data. GlobalLock(0x%p) failed: 0x%lx."), hdata, error_code);
942 return NULL;
943 }
944
945 length = GlobalSize (hdata);
946 if (length == 0 && GetLastError () != NO_ERROR)
947 {
948 DWORD error_code = GetLastError ();
949 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
950 _("Cannot get DnD data. GlobalSize(0x%p) failed: 0x%lx."), hdata, error_code);
951 GlobalUnlock (hdata);
952 return NULL;
953 }
954
955 data = g_try_malloc (length);
956
957 if (data == NULL)
958 {
959 char *length_str = g_strdup_printf ("%" G_GSIZE_FORMAT, length);
960 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
961 _("Cannot get DnD data. Failed to allocate %s bytes to store the data."), length_str);
962 g_free (length_str);
963 GlobalUnlock (hdata);
964 return NULL;
965 }
966
967 memcpy (data, ptr, length);
968 *data_len = length;
969
970 GlobalUnlock (hdata);
971
972 return data;
973 }
974
975 static void
976 gdk_win32_drop_read_async (GdkDrop *drop,
977 GdkContentFormats *formats,
978 int io_priority,
979 GCancellable *cancellable,
980 GAsyncReadyCallback callback,
981 gpointer user_data)
982 {
983 GdkWin32Drop *drop_win32 = GDK_WIN32_DROP (drop);
984 GTask *task;
985 drop_target_context *tctx;
986 const char * const *mime_types;
987 gsize i, j, n_mime_types;
988 GdkWin32ContentFormatPair *pair;
989 FORMATETC fmt;
990 HRESULT hr;
991 STGMEDIUM storage;
992 guchar *data;
993 gsize data_len;
994 GInputStream *stream;
995
996 task = g_task_new (drop, cancellable, callback, user_data);
997 g_task_set_priority (task, io_priority);
998 g_task_set_source_tag (task, gdk_win32_drop_read_async);
999
1000 mime_types = gdk_content_formats_get_mime_types (formats, &n_mime_types);
1001
1002 if (drop_win32->protocol == GDK_DRAG_PROTO_WIN32_DROPFILES)
1003 {
1004 for (i = 0; i < n_mime_types; i++)
1005 if (g_strcmp0 (mime_types[i], "text/uri-list") == 0)
1006 break;
1007 if (i >= n_mime_types)
1008 {
1009 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1010 _("No compatible transfer format found"));
1011 g_object_unref (task);
1012 g_clear_pointer (&drop_win32->dropfiles_list, g_free);
1013
1014 return;
1015 }
1016
1017 stream = g_memory_input_stream_new_from_data (drop_win32->dropfiles_list, strlen (drop_win32->dropfiles_list), g_free);
1018 drop_win32->dropfiles_list = NULL;
1019 g_object_set_data (G_OBJECT (stream), "gdk-dnd-stream-contenttype", (gpointer) "text/uri-list");
1020 g_task_return_pointer (task, stream, g_object_unref);
1021 g_object_unref (task);
1022
1023 return;
1024 }
1025
1026 tctx = GDK_WIN32_SURFACE (gdk_drop_get_surface (drop))->drop_target;
1027
1028 if (tctx == NULL)
1029 {
1030 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
1031 _("GDK surface 0x%p is not registered as a drop target"), gdk_drop_get_surface (drop));
1032 g_object_unref (task);
1033 return;
1034 }
1035
1036 if (tctx->data_object == NULL)
1037 {
1038 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
1039 _("Target context record 0x%p has no data object"), tctx);
1040 g_object_unref (task);
1041 return;
1042 }
1043
1044 for (pair = NULL, i = 0; i < n_mime_types; i++)
1045 {
1046 for (j = 0; j < drop_win32->droptarget_w32format_contentformat_map->len; j++)
1047 {
1048 pair = &g_array_index (drop_win32->droptarget_w32format_contentformat_map, GdkWin32ContentFormatPair, j);
1049 if (pair->contentformat == mime_types[i])
1050 break;
1051
1052 pair = NULL;
1053 }
1054 }
1055
1056 if (pair == NULL)
1057 {
1058 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
1059 _("No compatible transfer format found"));
1060 g_object_unref (task);
1061 return;
1062 }
1063
1064 fmt.cfFormat = pair->w32format;
1065 if (_gdk_win32_format_uses_hdata (pair->w32format))
1066 fmt.tymed = TYMED_HGLOBAL;
1067 else
1068 g_assert_not_reached ();
1069
1070 fmt.ptd = NULL;
1071 fmt.dwAspect = DVASPECT_CONTENT;
1072 fmt.lindex = -1;
1073
1074 hr = IDataObject_GetData (tctx->data_object, &fmt, &storage);
1075
1076 if (!SUCCEEDED (hr) || hr != S_OK)
1077 {
1078 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
1079 _("IDataObject_GetData (0x%x) failed, returning 0x%lx"), fmt.cfFormat, hr);
1080 g_object_unref (task);
1081 return;
1082 }
1083
1084 if (!pair->transmute)
1085 {
1086 if (_gdk_win32_format_uses_hdata (pair->w32format))
1087 {
1088 data = grab_data_from_hdata (task, storage.hGlobal, &data_len);
1089
1090 if (data == NULL)
1091 {
1092 ReleaseStgMedium (&storage);
1093 g_object_unref (task);
1094
1095 return;
1096 }
1097 }
1098 else
1099 {
1100 g_assert_not_reached ();
1101 }
1102 }
1103 else
1104 {
1105 GdkDisplay *display = gdk_drop_get_display (drop);
1106 gdk_win32_clipdrop_transmute_windows_data (gdk_win32_display_get_clipdrop (display),
1107 pair->w32format, pair->contentformat,
1108 storage.hGlobal,
1109 &data,
1110 &data_len);
1111 }
1112
1113 ReleaseStgMedium (&storage);
1114
1115 if (data == NULL)
1116 {
1117 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
1118 _("Failed to transmute DnD data W32 format 0x%x to %p (%s)"), pair->w32format, pair->contentformat, pair->contentformat);
1119 g_object_unref (task);
1120 return;
1121 }
1122
1123 stream = g_memory_input_stream_new_from_data (data, data_len, g_free);
1124 g_object_set_data (G_OBJECT (stream), "gdk-dnd-stream-contenttype", (gpointer) pair->contentformat);
1125 g_task_return_pointer (task, stream, g_object_unref);
1126 g_object_unref (task);
1127 }
1128
1129 static GInputStream *
1130 gdk_win32_drop_read_finish (GdkDrop *drop,
1131 GAsyncResult *result,
1132 const char **out_mime_type,
1133 GError **error)
1134 {
1135 GTask *task;
1136 GInputStream *stream;
1137
1138 g_return_val_if_fail (g_task_is_valid (result, G_OBJECT (drop)), NULL);
1139 task = G_TASK (result);
1140 g_return_val_if_fail (g_task_get_source_tag (task) == gdk_win32_drop_read_async, NULL);
1141
1142 stream = g_task_propagate_pointer (task, error);
1143
1144 if (stream && out_mime_type)
1145 *out_mime_type = g_object_get_data (G_OBJECT (stream), "gdk-dnd-stream-contenttype");
1146
1147 return stream;
1148 }
1149
1150 static void
1151 gdk_win32_drop_class_init (GdkWin32DropClass *klass)
1152 {
1153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1154 GdkDropClass *drop_class = GDK_DROP_CLASS (klass);
1155
1156 object_class->finalize = gdk_win32_drop_finalize;
1157
1158 drop_class->status = gdk_win32_drop_status;
1159 drop_class->finish = gdk_win32_drop_finish;
1160 drop_class->read_async = gdk_win32_drop_read_async;
1161 drop_class->read_finish = gdk_win32_drop_read_finish;
1162 }