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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 /*
23 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
24 * file for a list of people on the GTK+ Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 */
28
29 #include "config.h"
30 #include <string.h>
31
32 #include <io.h>
33 #include <fcntl.h>
34
35 /*
36 * Comment from the old OLE2 DND code that is being merged in. Note
37 * that this comment might not fully reflect reality as the code
38 * obviously will have to be modified in this merge. Especially the
39 * talk about supporting other than UTF-8 text is bogus, that will not
40 * happen.
41 *
42 * Support for OLE-2 drag and drop added at Archaeopteryx Software, 2001
43 * For more information, contact Stephan R.A. Deibel ([email protected])
44 *
45 * Notes on implementation:
46 *
47 * This is a first pass at OLE2 support. It only supports text and unicode text
48 * data types, and file list dnd (which is handled seperately as it predates OLE2
49 * both in this implementation and on Windows in general).
50 *
51 * As such, the data type conversion from gdk selection targets to OLE2 CF_* data
52 * type specifiers is partially hardwired. Fixing this is complicated by (a) the
53 * fact that the widget's declared selection types aren't accessible in calls here
54 * that need to declare the corresponding OLE2 data types, and (b) there isn't a
55 * 1-1 correspondence between gdk target types and OLE2 types. The former needs
56 * some redesign in gtk dnd (something a gdk/gtk expert should do; I have tried
57 * and failed!). As an example of the latter: gdk STRING, TEXT, COMPOUND_TEXT map
58 * to CF_TEXT, CF_OEMTEXT, and CF_UNICODETEXT but as a group and with conversions
59 * necessary for various combinations. Currently, the code here (and in
60 * gdkdnd-win32.c) can handle gdk STRING and TEXT but not COMPOUND_TEXT, and OLE2
61 * CF_TEXT and CF_UNICODETEXT but not CF_OEMTEXT. The necessary conversions are
62 * supplied by the implementation here.
63 *
64 * Note that in combination with another hack originated by Archaeopteryx
65 * Software, the text conversions here may go to utf-8 unicode as the standard
66 * within-gtk target or to single-byte ascii when the USE_ACP_TEXT compilation
67 * flag is TRUE. This mode was added to support applications that aren't using
68 * utf-8 across the gtk/gdk API but instead use single-byte ascii according to
69 * the current Windows code page. See gdkim-win32.c for more info on that.
70 *
71 */
72
73 /* The mingw.org compiler does not export GUIDS in it's import library. To work
74 * around that, define INITGUID to have the GUIDS declared. */
75 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
76 #define INITGUID
77 #endif
78
79 #include "gdkdnd.h"
80 #include "gdkproperty.h"
81 #include "gdkinternals.h"
82 #include "gdkprivate-win32.h"
83
84 #include <ole2.h>
85
86 #include <shlobj.h>
87 #include <shlguid.h>
88
89 #include <gdk/gdk.h>
90 #include <glib/gstdio.h>
91
92 typedef struct _GdkDragContextPrivateWin32 GdkDragContextPrivateWin32;
93
94 typedef enum {
95 GDK_DRAG_STATUS_DRAG,
96 GDK_DRAG_STATUS_MOTION_WAIT,
97 GDK_DRAG_STATUS_ACTION_WAIT,
98 GDK_DRAG_STATUS_DROP
99 } GdkDragStatus;
100
101 /* Structure that holds information about a drag in progress.
102 * this is used on both source and destination sides.
103 */
104 struct _GdkDragContextPrivateWin32 {
105 gboolean being_finalized;
106 gint ref_count;
107 IUnknown *iface;
108 DWORD last_key_state;
109 POINT last_pt; /* Coordinates from last event */
110 guint drag_status : 4; /* Current status of drag */
111 guint drop_failed : 1; /* Whether the drop was unsuccessful */
112 };
113
114 #define PRIVATE_DATA(context) ((GdkDragContextPrivateWin32 *) GDK_DRAG_CONTEXT (context)->windowing_data)
115
116 static GList *contexts;
117 static GdkDragContext *current_dest_drag = NULL;
118
119 static void gdk_drag_context_init (GdkDragContext *dragcontext);
120 static void gdk_drag_context_class_init (GdkDragContextClass *klass);
121 static void gdk_drag_context_finalize (GObject *object);
122
123 static gpointer parent_class = NULL;
124
125 static gboolean use_ole2_dnd = FALSE;
126
127 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
128
129 static void
130 gdk_drag_context_init (GdkDragContext *dragcontext)
131 {
132 GdkDragContextPrivateWin32 *private;
133
134 private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
135 GDK_TYPE_DRAG_CONTEXT,
136 GdkDragContextPrivateWin32);
137
138 dragcontext->windowing_data = private;
139
140 if (!use_ole2_dnd)
141 {
142 contexts = g_list_prepend (contexts, dragcontext);
143 }
144 else
145 {
146 private->being_finalized = FALSE;
147 private->ref_count = 1;
148 private->iface = NULL;
149 }
150
151 GDK_NOTE (DND, g_print ("gdk_drag_context_init %p\n", dragcontext));
152 }
153
154 static void
155 gdk_drag_context_class_init (GdkDragContextClass *klass)
156 {
157 GObjectClass *object_class = G_OBJECT_CLASS (klass);
158
159 parent_class = g_type_class_peek_parent (klass);
160
161 object_class->finalize = gdk_drag_context_finalize;
162
163 g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateWin32));
164 }
165
166 static void
167 gdk_drag_context_finalize (GObject *object)
168 {
169 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
170
171 GDK_NOTE (DND, g_print ("gdk_drag_context_finalize %p\n", object));
172
173 g_list_free (context->targets);
174
175 if (context->source_window)
176 g_object_unref (context->source_window);
177
178 if (context->dest_window)
179 g_object_unref (context->dest_window);
180
181 if (!use_ole2_dnd)
182 {
183 contexts = g_list_remove (contexts, context);
184
185 if (context == current_dest_drag)
186 current_dest_drag = NULL;
187 }
188 else
189 {
190 GdkDragContextPrivateWin32 *private = PRIVATE_DATA (context);
191 if (private->iface)
192 {
193 private->being_finalized = TRUE;
194 private->iface->lpVtbl->Release (private->iface);
195 private->iface = NULL;
196 }
197 }
198
199 G_OBJECT_CLASS (parent_class)->finalize (object);
200 }
201
202 /* Drag Contexts */
203
204 GdkDragContext *
205 gdk_drag_context_new (void)
206 {
207 return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
208 }
209
210 void
211 gdk_drag_context_ref (GdkDragContext *context)
212 {
213 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
214
215 g_object_ref (context);
216 }
217
218 void
219 gdk_drag_context_unref (GdkDragContext *context)
220 {
221 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
222
223 g_object_unref (context);
224 }
225
226 static GdkDragContext *
227 gdk_drag_context_find (gboolean is_source,
228 GdkWindow *source,
229 GdkWindow *dest)
230 {
231 GList *tmp_list = contexts;
232 GdkDragContext *context;
233 GdkDragContextPrivateWin32 *private;
234
235 while (tmp_list)
236 {
237 context = (GdkDragContext *)tmp_list->data;
238 private = PRIVATE_DATA (context);
239
240 if ((!context->is_source == !is_source) &&
241 ((source == NULL) || (context->source_window && (context->source_window == source))) &&
242 ((dest == NULL) || (context->dest_window && (context->dest_window == dest))))
243 return context;
244
245 tmp_list = tmp_list->next;
246 }
247
248 return NULL;
249 }
250
251 #define PRINT_GUID(guid) \
252 g_print ("%.08lx-%.04x-%.04x-%.02x%.02x-%.02x%.02x%.02x%.02x%.02x%.02x", \
253 ((gulong *) guid)[0], \
254 ((gushort *) guid)[2], \
255 ((gushort *) guid)[3], \
256 ((guchar *) guid)[8], \
257 ((guchar *) guid)[9], \
258 ((guchar *) guid)[10], \
259 ((guchar *) guid)[11], \
260 ((guchar *) guid)[12], \
261 ((guchar *) guid)[13], \
262 ((guchar *) guid)[14], \
263 ((guchar *) guid)[15]);
264
265
266 static FORMATETC *formats;
267 static int nformats;
268
269 typedef struct {
270 IDropTarget idt;
271 GdkDragContext *context;
272 } target_drag_context;
273
274 typedef struct {
275 IDropSource ids;
276 GdkDragContext *context;
277 } source_drag_context;
278
279 typedef struct {
280 IDataObject ido;
281 int ref_count;
282 GdkDragContext *context;
283 } data_object;
284
285 typedef struct {
286 IEnumFORMATETC ief;
287 int ref_count;
288 int ix;
289 } enum_formats;
290
291 static source_drag_context *pending_src_context = NULL;
292 static IDataObject *dnd_data = NULL;
293
294 static enum_formats *enum_formats_new (void);
295
296 /* map windows -> target drag contexts. The table
297 * owns a ref to both objects.
298 */
299 static GHashTable* target_ctx_for_window = NULL;
300
301 static ULONG STDMETHODCALLTYPE
302 idroptarget_addref (LPDROPTARGET This)
303 {
304 target_drag_context *ctx = (target_drag_context *) This;
305 GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx->context);
306 int ref_count = ++private->ref_count;
307
308 GDK_NOTE (DND, g_print ("idroptarget_addref %p %d\n", This, ref_count));
309 g_object_ref (G_OBJECT (ctx->context));
310
311 return ref_count;
312 }
313
314 static HRESULT STDMETHODCALLTYPE
315 idroptarget_queryinterface (LPDROPTARGET This,
316 REFIID riid,
317 LPVOID *ppvObject)
318 {
319 GDK_NOTE (DND, {
320 g_print ("idroptarget_queryinterface %p ", This);
321 PRINT_GUID (riid);
322 });
323
324 *ppvObject = NULL;
325
326 if (IsEqualGUID (riid, &IID_IUnknown))
327 {
328 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
329 idroptarget_addref (This);
330 *ppvObject = This;
331 return S_OK;
332 }
333 else if (IsEqualGUID (riid, &IID_IDropTarget))
334 {
335 GDK_NOTE (DND, g_print ("...IDropTarget S_OK\n"));
336 idroptarget_addref (This);
337 *ppvObject = This;
338 return S_OK;
339 }
340 else
341 {
342 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
343 return E_NOINTERFACE;
344 }
345 }
346
347 static ULONG STDMETHODCALLTYPE
348 idroptarget_release (LPDROPTARGET This)
349 {
350 target_drag_context *ctx = (target_drag_context *) This;
351 GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx->context);
352 int ref_count = --private->ref_count;
353
354 GDK_NOTE (DND, g_print ("idroptarget_release %p %d\n", This, ref_count));
355
356 if (!private->being_finalized)
357 g_object_unref (G_OBJECT (ctx->context));
358
359 if (ref_count == 0)
360 g_free (This);
361
362 return ref_count;
363 }
364
365 #if 0
366
367 static GdkAtom
368 cf_to_atom (CLIPFORMAT cf)
369 {
370 switch (cf)
371 {
372 case CF_UNICODETEXT:
373 return _utf8_string;
374 case CF_HDROP:
375 return _text_uri_list;
376 case CF_DIB:
377 return _image_bmp;
378 }
379
380 if (cf == _cf_url)
381 return _text_uri_list;
382
383 if (cf == _cf_html_format || cf == _cf_text_html)
384 return _text_html;
385
386 return GDK_NONE;
387 }
388
389 #endif
390
391 static GdkDragAction
392 get_suggested_action (DWORD grfKeyState)
393 {
394 /* This is the yucky Windows standard: Force link action if both
395 * Control and Alt are down, copy if Control is down alone, move if
396 * Alt is down alone, or use default of move within the app or copy
397 * when origin of the drag is in another app.
398 */
399 if (grfKeyState & MK_CONTROL && grfKeyState & MK_SHIFT)
400 return GDK_ACTION_LINK; /* Link action not supported */
401 else if (grfKeyState & MK_CONTROL)
402 return GDK_ACTION_COPY;
403 else if (grfKeyState & MK_ALT)
404 return GDK_ACTION_MOVE;
405 #if 0 /* Default is always copy for now */
406 else if (_dnd_source_state == GDK_WIN32_DND_DRAGGING)
407 return GDK_ACTION_MOVE;
408 #endif
409 else
410 return GDK_ACTION_COPY;
411 /* Any way to determine when to add in DROPEFFECT_SCROLL? */
412 }
413
414 /* Process pending events -- we don't want to service non-GUI events
415 * forever so do one iteration and then do more only if there's a
416 * pending GDK event.
417 */
418 static void
419 process_pending_events ()
420 {
421 g_main_context_iteration (NULL, FALSE);
422 while (_gdk_event_queue_find_first (_gdk_display))
423 g_main_context_iteration (NULL, FALSE);
424 }
425
426 static DWORD
427 drop_effect_for_action (GdkDragAction action)
428 {
429 switch (action)
430 {
431 case GDK_ACTION_MOVE:
432 return DROPEFFECT_MOVE;
433 case GDK_ACTION_LINK:
434 return DROPEFFECT_LINK;
435 case GDK_ACTION_COPY:
436 return DROPEFFECT_COPY;
437 default:
438 return DROPEFFECT_NONE;
439 }
440 }
441
442 static void
443 dnd_event_put (GdkEventType type,
444 GdkDragContext *context,
445 const POINTL pt,
446 gboolean to_dest_window)
447 {
448 GdkEvent e;
449 e.type = type;
450 if (to_dest_window)
451 e.dnd.window = context->dest_window;
452 else
453 e.dnd.window = context->source_window;
454 e.dnd.send_event = FALSE;
455 e.dnd.context = context;
456 e.dnd.time = GDK_CURRENT_TIME;
457 e.dnd.x_root = pt.x + _gdk_offset_x;
458 e.dnd.y_root = pt.x + _gdk_offset_y;
459
460 g_object_ref (e.dnd.context);
461 if (e.dnd.window != NULL)
462 g_object_ref (e.dnd.window);
463
464 GDK_NOTE (EVENTS, _gdk_win32_print_event (&e));
465 gdk_event_put (&e);
466 }
467
468 static HRESULT STDMETHODCALLTYPE
469 idroptarget_dragenter (LPDROPTARGET This,
470 LPDATAOBJECT pDataObj,
471 DWORD grfKeyState,
472 POINTL pt,
473 LPDWORD pdwEffect)
474 {
475 target_drag_context *ctx = (target_drag_context *) This;
476
477 GDK_NOTE (DND, g_print ("idroptarget_dragenter %p S_OK\n", This));
478
479 ctx->context->suggested_action = get_suggested_action (grfKeyState);
480 dnd_event_put (GDK_DRAG_ENTER, ctx->context, pt, TRUE);
481 process_pending_events ();
482 *pdwEffect = drop_effect_for_action (ctx->context->action);
483
484 /* Assume that target can accept the data: In fact it may fail but
485 * we are not really set up to query the target!
486 */
487 return S_OK;
488 }
489
490 static HRESULT STDMETHODCALLTYPE
491 idroptarget_dragover (LPDROPTARGET This,
492 DWORD grfKeyState,
493 POINTL pt,
494 LPDWORD pdwEffect)
495 {
496 target_drag_context *ctx = (target_drag_context *) This;
497
498 GDK_NOTE (DND, g_print ("idroptarget_dragover %p S_OK\n", This));
499
500 ctx->context->suggested_action = get_suggested_action (grfKeyState);
501 dnd_event_put (GDK_DRAG_MOTION, ctx->context, pt, TRUE);
502 process_pending_events ();
503 *pdwEffect = drop_effect_for_action (ctx->context->action);
504
505 return S_OK;
506 }
507
508 static HRESULT STDMETHODCALLTYPE
509 idroptarget_dragleave (LPDROPTARGET This)
510 {
511 target_drag_context *ctx = (target_drag_context *) This;
512 POINTL pt = { 0, 0 };
513
514 GDK_NOTE (DND, g_print ("idroptarget_dragleave %p S_OK\n", This));
515
516 dnd_event_put (GDK_DRAG_LEAVE, ctx->context, pt, TRUE);
517 process_pending_events ();
518
519 return S_OK;
520 }
521
522 static HRESULT STDMETHODCALLTYPE
523 idroptarget_drop (LPDROPTARGET This,
524 LPDATAOBJECT pDataObj,
525 DWORD grfKeyState,
526 POINTL pt,
527 LPDWORD pdwEffect)
528 {
529 target_drag_context *ctx = (target_drag_context *) This;
530
531 GDK_NOTE (DND, g_print ("idroptarget_drop %p ", This));
532
533 if (pDataObj == NULL)
534 {
535 GDK_NOTE (DND, g_print ("E_POINTER\n"));
536 return E_POINTER;
537 }
538
539 dnd_data = pDataObj;
540
541 ctx->context->suggested_action = get_suggested_action (grfKeyState);
542 dnd_event_put (GDK_DROP_START, ctx->context, pt, TRUE);
543 process_pending_events ();
544
545 dnd_data = NULL;
546
547 /* Notify OLE of copy or move */
548 if (_dnd_target_state != GDK_WIN32_DND_DROPPED)
549 *pdwEffect = DROPEFFECT_NONE;
550 else
551 *pdwEffect = drop_effect_for_action (ctx->context->action);
552
553 GDK_NOTE (DND, g_print ("S_OK\n"));
554
555 return S_OK;
556 }
557
558 static ULONG STDMETHODCALLTYPE
559 idropsource_addref (LPDROPSOURCE This)
560 {
561 source_drag_context *ctx = (source_drag_context *) This;
562 GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx->context);
563 int ref_count = ++private->ref_count;
564
565 GDK_NOTE (DND, g_print ("idropsource_addref %p %d\n", This, ref_count));
566 g_object_ref (G_OBJECT (ctx->context));
567
568 return ref_count;
569 }
570
571 static HRESULT STDMETHODCALLTYPE
572 idropsource_queryinterface (LPDROPSOURCE This,
573 REFIID riid,
574 LPVOID *ppvObject)
575 {
576 GDK_NOTE (DND, {
577 g_print ("idropsource_queryinterface %p ", This);
578 PRINT_GUID (riid);
579 });
580
581 *ppvObject = NULL;
582
583 if (IsEqualGUID (riid, &IID_IUnknown))
584 {
585 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
586 idropsource_addref (This);
587 *ppvObject = This;
588 return S_OK;
589 }
590 else if (IsEqualGUID (riid, &IID_IDropSource))
591 {
592 GDK_NOTE (DND, g_print ("...IDropSource S_OK\n"));
593 idropsource_addref (This);
594 *ppvObject = This;
595 return S_OK;
596 }
597 else
598 {
599 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
600 return E_NOINTERFACE;
601 }
602 }
603
604 static ULONG STDMETHODCALLTYPE
605 idropsource_release (LPDROPSOURCE This)
606 {
607 source_drag_context *ctx = (source_drag_context *) This;
608 GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx->context);
609 int ref_count = --private->ref_count;
610
611 GDK_NOTE (DND, g_print ("idropsource_release %p %d\n", This, ref_count));
612
613 if (!private->being_finalized)
614 g_object_unref (G_OBJECT (ctx->context));
615
616 if (ref_count == 0)
617 g_free (This);
618
619 return ref_count;
620 }
621
622 /* Emit GDK events for any changes in mouse events or control key
623 * state since the last recorded state. Return true if any events
624 * have been emitted and false otherwise.
625 */
626 static gboolean
627 send_change_events (GdkDragContext *ctx,
628 DWORD key_state,
629 gboolean esc_pressed)
630 {
631 GdkDragContextPrivateWin32 *private = PRIVATE_DATA (ctx);
632 POINT pt;
633 gboolean changed = FALSE;
634 HWND hwnd = GDK_WINDOW_HWND (ctx->source_window);
635 LPARAM lparam;
636 WPARAM wparam;
637
638 if (!API_CALL (GetCursorPos, (&pt)))
639 return FALSE;
640
641 if (!API_CALL (ScreenToClient, (hwnd, &pt)))
642 return FALSE;
643
644 if (pt.x != private->last_pt.x || pt.y != private->last_pt.y ||
645 key_state != private->last_key_state)
646 {
647 lparam = MAKELPARAM (pt.x, pt.y);
648 wparam = key_state;
649 if (pt.x != private->last_pt.x || pt.y != private->last_pt.y)
650 {
651 GDK_NOTE (DND, g_print ("Sending WM_MOUSEMOVE (%ld,%ld)\n", pt.x, pt.y));
652 SendMessage (hwnd, WM_MOUSEMOVE, wparam, lparam);
653 }
654
655 if ((key_state & MK_LBUTTON) != (private->last_key_state & MK_LBUTTON))
656 {
657 if (key_state & MK_LBUTTON)
658 SendMessage (hwnd, WM_LBUTTONDOWN, wparam, lparam);
659 else
660 SendMessage (hwnd, WM_LBUTTONUP, wparam, lparam);
661 }
662 if ((key_state & MK_MBUTTON) != (private->last_key_state & MK_MBUTTON))
663 {
664 if (key_state & MK_MBUTTON)
665 SendMessage (hwnd, WM_MBUTTONDOWN, wparam, lparam);
666 else
667 SendMessage (hwnd, WM_MBUTTONUP, wparam, lparam);
668 }
669 if ((key_state & MK_RBUTTON) != (private->last_key_state & MK_RBUTTON))
670 {
671 if (key_state & MK_RBUTTON)
672 SendMessage (hwnd, WM_RBUTTONDOWN, wparam, lparam);
673 else
674 SendMessage (hwnd, WM_RBUTTONUP, wparam, lparam);
675 }
676 if ((key_state & MK_CONTROL) != (private->last_key_state & MK_CONTROL))
677 {
678 if (key_state & MK_CONTROL)
679 SendMessage (hwnd, WM_KEYDOWN, VK_CONTROL, 0);
680 else
681 SendMessage (hwnd, WM_KEYUP, VK_CONTROL, 0);
682 }
683 if ((key_state & MK_SHIFT) != (private->last_key_state & MK_SHIFT))
684 {
685 if (key_state & MK_CONTROL)
686 SendMessage (hwnd, WM_KEYDOWN, VK_SHIFT, 0);
687 else
688 SendMessage (hwnd, WM_KEYUP, VK_SHIFT, 0);
689 }
690
691 changed = TRUE;
692 private->last_key_state = key_state;
693 private->last_pt = pt;
694 }
695
696 if (esc_pressed)
697 {
698 GDK_NOTE (DND, g_print ("Sending a escape key down message to %p\n", hwnd));
699 SendMessage (hwnd, WM_KEYDOWN, VK_ESCAPE, 0);
700 changed = TRUE;
701 }
702
703 return changed;
704 }
705
706 static HRESULT STDMETHODCALLTYPE
707 idropsource_querycontinuedrag (LPDROPSOURCE This,
708 BOOL fEscapePressed,
709 DWORD grfKeyState)
710 {
711 source_drag_context *ctx = (source_drag_context *) This;
712
713 GDK_NOTE (DND, g_print ("idropsource_querycontinuedrag %p ", This));
714
715 if (send_change_events (ctx->context, grfKeyState, fEscapePressed))
716 process_pending_events ();
717
718 if (_dnd_source_state == GDK_WIN32_DND_DROPPED)
719 {
720 GDK_NOTE (DND, g_print ("DRAGDROP_S_DROP\n"));
721 return DRAGDROP_S_DROP;
722 }
723 else if (_dnd_source_state == GDK_WIN32_DND_NONE)
724 {
725 GDK_NOTE (DND, g_print ("DRAGDROP_S_CANCEL\n"));
726 return DRAGDROP_S_CANCEL;
727 }
728 else
729 {
730 GDK_NOTE (DND, g_print ("S_OK\n"));
731 return S_OK;
732 }
733 }
734
735 static HRESULT STDMETHODCALLTYPE
736 idropsource_givefeedback (LPDROPSOURCE This,
737 DWORD dwEffect)
738 {
739 source_drag_context *ctx = (source_drag_context *) This;
740 GdkDragAction suggested_action;
741
742 GDK_NOTE (DND, g_print ("idropsource_givefeedback %p DRAGDROP_S_USEDEFAULTCURSORS\n", This));
743
744 if (dwEffect == DROPEFFECT_MOVE)
745 suggested_action = GDK_ACTION_MOVE;
746 else
747 suggested_action = GDK_ACTION_COPY;
748 ctx->context->action = suggested_action;
749
750 if (dwEffect == DROPEFFECT_NONE)
751 {
752 if (ctx->context->dest_window != NULL)
753 {
754 g_object_unref (ctx->context->dest_window);
755 ctx->context->dest_window = NULL;
756 }
757 }
758 else
759 {
760 if (ctx->context->dest_window == NULL)
761 ctx->context->dest_window = g_object_ref (_gdk_root);
762 }
763
764 return DRAGDROP_S_USEDEFAULTCURSORS;
765 }
766
767 static ULONG STDMETHODCALLTYPE
768 idataobject_addref (LPDATAOBJECT This)
769 {
770 data_object *dobj = (data_object *) This;
771 int ref_count = ++dobj->ref_count;
772
773 GDK_NOTE (DND, g_print ("idataobject_addref %p %d\n", This, ref_count));
774
775 return ref_count;
776 }
777
778 static HRESULT STDMETHODCALLTYPE
779 idataobject_queryinterface (LPDATAOBJECT This,
780 REFIID riid,
781 LPVOID *ppvObject)
782 {
783 GDK_NOTE (DND, {
784 g_print ("idataobject_queryinterface %p ", This);
785 PRINT_GUID (riid);
786 });
787
788 *ppvObject = NULL;
789
790 if (IsEqualGUID (riid, &IID_IUnknown))
791 {
792 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
793 idataobject_addref (This);
794 *ppvObject = This;
795 return S_OK;
796 }
797 else if (IsEqualGUID (riid, &IID_IDataObject))
798 {
799 GDK_NOTE (DND, g_print ("...IDataObject S_OK\n"));
800 idataobject_addref (This);
801 *ppvObject = This;
802 return S_OK;
803 }
804 else
805 {
806 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
807 return E_NOINTERFACE;
808 }
809 }
810
811 static ULONG STDMETHODCALLTYPE
812 idataobject_release (LPDATAOBJECT This)
813 {
814 data_object *dobj = (data_object *) This;
815 int ref_count = --dobj->ref_count;
816
817 GDK_NOTE (DND, g_print ("idataobject_release %p %d\n", This, ref_count));
818
819 if (ref_count == 0)
820 g_free (This);
821
822 return ref_count;
823 }
824
825 static HRESULT
826 query (LPDATAOBJECT This,
827 LPFORMATETC pFormatEtc)
828 {
829 int i;
830
831 if (!pFormatEtc)
832 return DV_E_FORMATETC;
833
834 if (pFormatEtc->lindex != -1)
835 return DV_E_LINDEX;
836
837 if ((pFormatEtc->tymed & TYMED_HGLOBAL) == 0)
838 return DV_E_TYMED;
839
840 if ((pFormatEtc->dwAspect & DVASPECT_CONTENT) == 0)
841 return DV_E_DVASPECT;
842
843 for (i = 0; i < nformats; i++)
844 if (pFormatEtc->cfFormat == formats[i].cfFormat)
845 return S_OK;
846
847 return DV_E_FORMATETC;
848 }
849
850 static FORMATETC *active_pFormatEtc = NULL;
851 static STGMEDIUM *active_pMedium = NULL;
852
853 static HRESULT STDMETHODCALLTYPE
854 idataobject_getdata (LPDATAOBJECT This,
855 LPFORMATETC pFormatEtc,
856 LPSTGMEDIUM pMedium)
857 {
858 data_object *ctx = (data_object *) This;
859 GdkAtom target;
860 HRESULT hr;
861 GdkEvent e;
862
863 GDK_NOTE (DND, g_print ("idataobject_getdata %p %s ",
864 This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
865
866 /* Check whether we can provide requested format */
867 hr = query (This, pFormatEtc);
868 if (hr != S_OK)
869 return hr;
870
871 /* Append a GDK_SELECTION_GET event and then hope the app sets the
872 * property associated with the _gdk_ole2_dnd atom
873 */
874
875 active_pFormatEtc = pFormatEtc;
876 active_pMedium = pMedium;
877
878 target = GDK_TARGET_STRING;
879
880 e.type = GDK_SELECTION_REQUEST;
881 e.selection.window = ctx->context->source_window;
882 e.selection.send_event = FALSE; /* ??? */
883 /* FIXME: Should really both selection and property be _gdk_ole2_dnd? */
884 e.selection.selection = _gdk_ole2_dnd;
885 /* FIXME: Target? */
886 e.selection.target = _utf8_string;
887 e.selection.property = _gdk_ole2_dnd;
888 e.selection.time = GDK_CURRENT_TIME;
889
890 g_object_ref (e.selection.window);
891
892 GDK_NOTE (EVENTS, _gdk_win32_print_event (&e));
893 gdk_event_put (&e);
894 process_pending_events ();
895
896 active_pFormatEtc = NULL;
897 active_pMedium = NULL;
898
899 if (pMedium->hGlobal == NULL) {
900 return E_UNEXPECTED;
901 }
902
903 return S_OK;
904 }
905
906 static HRESULT STDMETHODCALLTYPE
907 idataobject_getdatahere (LPDATAOBJECT This,
908 LPFORMATETC pFormatEtc,
909 LPSTGMEDIUM pMedium)
910 {
911 GDK_NOTE (DND, g_print ("idataobject_getdatahere %p %s E_UNEXPECTED\n",
912 This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
913
914 return E_UNEXPECTED;
915 }
916
917 static HRESULT STDMETHODCALLTYPE
918 idataobject_querygetdata (LPDATAOBJECT This,
919 LPFORMATETC pFormatEtc)
920 {
921 HRESULT hr;
922
923 hr = query (This, pFormatEtc);
924
925 #define CASE(x) case x: g_print (#x)
926 GDK_NOTE (DND, {
927 g_print ("idataobject_querygetdata %p %s \n",
928 This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat));
929 switch (hr)
930 {
931 CASE (DV_E_FORMATETC);
932 CASE (DV_E_LINDEX);
933 CASE (DV_E_TYMED);
934 CASE (DV_E_DVASPECT);
935 CASE (S_OK);
936 default: g_print ("%#lx", hr);
937 }
938 });
939
940 return hr;
941 }
942
943 static HRESULT STDMETHODCALLTYPE
944 idataobject_getcanonicalformatetc (LPDATAOBJECT This,
945 LPFORMATETC pFormatEtcIn,
946 LPFORMATETC pFormatEtcOut)
947 {
948 GDK_NOTE (DND, g_print ("idataobject_getcanonicalformatetc %p E_UNEXPECTED\n", This));
949
950 return E_UNEXPECTED;
951 }
952
953 static HRESULT STDMETHODCALLTYPE
954 idataobject_setdata (LPDATAOBJECT This,
955 LPFORMATETC pFormatEtc,
956 LPSTGMEDIUM pMedium,
957 BOOL fRelease)
958 {
959 GDK_NOTE (DND, g_print ("idataobject_setdata %p %s E_UNEXPECTED\n",
960 This, _gdk_win32_cf_to_string (pFormatEtc->cfFormat)));
961
962 return E_UNEXPECTED;
963 }
964
965 static HRESULT STDMETHODCALLTYPE
966 idataobject_enumformatetc (LPDATAOBJECT This,
967 DWORD dwDirection,
968 LPENUMFORMATETC *ppEnumFormatEtc)
969 {
970 GDK_NOTE (DND, g_print ("idataobject_enumformatetc %p ", This));
971
972 if (dwDirection != DATADIR_GET)
973 {
974 GDK_NOTE (DND, g_print ("E_NOTIMPL\n"));
975 return E_NOTIMPL;
976 }
977
978 *ppEnumFormatEtc = &enum_formats_new ()->ief;
979
980 GDK_NOTE (DND, g_print ("%p S_OK\n", *ppEnumFormatEtc));
981
982 return S_OK;
983 }
984
985 static HRESULT STDMETHODCALLTYPE
986 idataobject_dadvise (LPDATAOBJECT This,
987 LPFORMATETC pFormatetc,
988 DWORD advf,
989 LPADVISESINK pAdvSink,
990 DWORD *pdwConnection)
991 {
992 GDK_NOTE (DND, g_print ("idataobject_dadvise %p E_NOTIMPL\n", This));
993
994 return E_NOTIMPL;
995 }
996
997 static HRESULT STDMETHODCALLTYPE
998 idataobject_dunadvise (LPDATAOBJECT This,
999 DWORD dwConnection)
1000 {
1001 GDK_NOTE (DND, g_print ("idataobject_dunadvise %p E_NOTIMPL\n", This));
1002
1003 return E_NOTIMPL;
1004 }
1005
1006 static HRESULT STDMETHODCALLTYPE
1007 idataobject_enumdadvise (LPDATAOBJECT This,
1008 LPENUMSTATDATA *ppenumAdvise)
1009 {
1010 GDK_NOTE (DND, g_print ("idataobject_enumdadvise %p OLE_E_ADVISENOTSUPPORTED\n", This));
1011
1012 return OLE_E_ADVISENOTSUPPORTED;
1013 }
1014
1015 static ULONG STDMETHODCALLTYPE
1016 ienumformatetc_addref (LPENUMFORMATETC This)
1017 {
1018 enum_formats *en = (enum_formats *) This;
1019 int ref_count = ++en->ref_count;
1020
1021 GDK_NOTE (DND, g_print ("ienumformatetc_addref %p %d\n", This, ref_count));
1022
1023 return ref_count;
1024 }
1025
1026 static HRESULT STDMETHODCALLTYPE
1027 ienumformatetc_queryinterface (LPENUMFORMATETC This,
1028 REFIID riid,
1029 LPVOID *ppvObject)
1030 {
1031 GDK_NOTE (DND, {
1032 g_print ("ienumformatetc_queryinterface %p", This);
1033 PRINT_GUID (riid);
1034 });
1035
1036 *ppvObject = NULL;
1037
1038 if (IsEqualGUID (riid, &IID_IUnknown))
1039 {
1040 GDK_NOTE (DND, g_print ("...IUnknown S_OK\n"));
1041 ienumformatetc_addref (This);
1042 *ppvObject = This;
1043 return S_OK;
1044 }
1045 else if (IsEqualGUID (riid, &IID_IEnumFORMATETC))
1046 {
1047 GDK_NOTE (DND, g_print ("...IEnumFORMATETC S_OK\n"));
1048 ienumformatetc_addref (This);
1049 *ppvObject = This;
1050 return S_OK;
1051 }
1052 else
1053 {
1054 GDK_NOTE (DND, g_print ("...E_NOINTERFACE\n"));
1055 return E_NOINTERFACE;
1056 }
1057 }
1058
1059 static ULONG STDMETHODCALLTYPE
1060 ienumformatetc_release (LPENUMFORMATETC This)
1061 {
1062 enum_formats *en = (enum_formats *) This;
1063 int ref_count = --en->ref_count;
1064
1065 GDK_NOTE (DND, g_print ("ienumformatetc_release %p %d\n", This, ref_count));
1066
1067 if (ref_count == 0)
1068 g_free (This);
1069
1070 return ref_count;
1071 }
1072
1073 static HRESULT STDMETHODCALLTYPE
1074 ienumformatetc_next (LPENUMFORMATETC This,
1075 ULONG celt,
1076 LPFORMATETC elts,
1077 ULONG *nelt)
1078 {
1079 enum_formats *en = (enum_formats *) This;
1080 int i, n;
1081
1082 GDK_NOTE (DND, g_print ("ienumformatetc_next %p %d %ld ", This, en->ix, celt));
1083
1084 n = 0;
1085 for (i = 0; i < celt; i++)
1086 {
1087 if (en->ix >= nformats)
1088 break;
1089 elts[i] = formats[en->ix++];
1090 n++;
1091 }
1092
1093 if (nelt != NULL)
1094 *nelt = n;
1095
1096 GDK_NOTE (DND, g_print ("%s\n", (n == celt) ? "S_OK" : "S_FALSE"));
1097
1098 if (n == celt)
1099 return S_OK;
1100 else
1101 return S_FALSE;
1102 }
1103
1104 static HRESULT STDMETHODCALLTYPE
1105 ienumformatetc_skip (LPENUMFORMATETC This,
1106 ULONG celt)
1107 {
1108 enum_formats *en = (enum_formats *) This;
1109
1110 GDK_NOTE (DND, g_print ("ienumformatetc_skip %p %d %ld S_OK\n", This, en->ix, celt));
1111
1112 en->ix += celt;
1113
1114 return S_OK;
1115 }
1116
1117 static HRESULT STDMETHODCALLTYPE
1118 ienumformatetc_reset (LPENUMFORMATETC This)
1119 {
1120 enum_formats *en = (enum_formats *) This;
1121
1122 GDK_NOTE (DND, g_print ("ienumformatetc_reset %p S_OK\n", This));
1123
1124 en->ix = 0;
1125
1126 return S_OK;
1127 }
1128
1129 static HRESULT STDMETHODCALLTYPE
1130 ienumformatetc_clone (LPENUMFORMATETC This,
1131 LPENUMFORMATETC *ppEnumFormatEtc)
1132 {
1133 enum_formats *en = (enum_formats *) This;
1134 enum_formats *new;
1135
1136 GDK_NOTE (DND, g_print ("ienumformatetc_clone %p S_OK\n", This));
1137
1138 new = enum_formats_new ();
1139
1140 new->ix = en->ix;
1141
1142 *ppEnumFormatEtc = &new->ief;
1143
1144 return S_OK;
1145 }
1146
1147 static IDropTargetVtbl idt_vtbl = {
1148 idroptarget_queryinterface,
1149 idroptarget_addref,
1150 idroptarget_release,
1151 idroptarget_dragenter,
1152 idroptarget_dragover,
1153 idroptarget_dragleave,
1154 idroptarget_drop
1155 };
1156
1157 static IDropSourceVtbl ids_vtbl = {
1158 idropsource_queryinterface,
1159 idropsource_addref,
1160 idropsource_release,
1161 idropsource_querycontinuedrag,
1162 idropsource_givefeedback
1163 };
1164
1165 static IDataObjectVtbl ido_vtbl = {
1166 idataobject_queryinterface,
1167 idataobject_addref,
1168 idataobject_release,
1169 idataobject_getdata,
1170 idataobject_getdatahere,
1171 idataobject_querygetdata,
1172 idataobject_getcanonicalformatetc,
1173 idataobject_setdata,
1174 idataobject_enumformatetc,
1175 idataobject_dadvise,
1176 idataobject_dunadvise,
1177 idataobject_enumdadvise
1178 };
1179
1180 static IEnumFORMATETCVtbl ief_vtbl = {
1181 ienumformatetc_queryinterface,
1182 ienumformatetc_addref,
1183 ienumformatetc_release,
1184 ienumformatetc_next,
1185 ienumformatetc_skip,
1186 ienumformatetc_reset,
1187 ienumformatetc_clone
1188 };
1189
1190
1191 static target_drag_context *
1192 target_context_new (GdkWindow *window)
1193 {
1194 target_drag_context *result;
1195 GdkDragContextPrivateWin32 *private;
1196
1197 result = g_new0 (target_drag_context, 1);
1198
1199 result->idt.lpVtbl = &idt_vtbl;
1200
1201 result->context = gdk_drag_context_new ();
1202 result->context->protocol = GDK_DRAG_PROTO_OLE2;
1203 result->context->is_source = FALSE;
1204
1205 result->context->source_window = NULL;
1206
1207 result->context->dest_window = window;
1208 g_object_ref (window);
1209
1210 /* FIXME: result->context->targets? */
1211
1212 result->context->actions = GDK_ACTION_DEFAULT | GDK_ACTION_COPY | GDK_ACTION_MOVE;
1213 result->context->suggested_action = GDK_ACTION_MOVE;
1214 result->context->action = GDK_ACTION_MOVE;
1215
1216 private = result->context->windowing_data;
1217 private->iface = (IUnknown *) &result->idt;
1218 idroptarget_addref (&result->idt);
1219
1220 GDK_NOTE (DND, g_print ("target_context_new: %p\n", result));
1221
1222 return result;
1223 }
1224
1225 static source_drag_context *
1226 source_context_new (GdkWindow *window,
1227 GList *targets)
1228 {
1229 source_drag_context *result;
1230 GdkDragContextPrivateWin32 *private;
1231
1232 result = g_new0 (source_drag_context, 1);
1233
1234 result->ids.lpVtbl = &ids_vtbl;
1235
1236 result->context = gdk_drag_context_new ();
1237 result->context->protocol = GDK_DRAG_PROTO_OLE2;
1238 result->context->is_source = TRUE;
1239
1240 result->context->source_window = window;
1241 g_object_ref (window);
1242
1243 result->context->dest_window = NULL;
1244 result->context->targets = g_list_copy (targets);
1245
1246 private = result->context->windowing_data;
1247 private->iface = (IUnknown *) &result->ids;
1248 idropsource_addref (&result->ids);
1249
1250 GDK_NOTE (DND, g_print ("source_context_new: %p\n", result));
1251
1252 return result;
1253 }
1254
1255 static data_object *
1256 data_object_new (GdkDragContext *context)
1257 {
1258 data_object *result;
1259
1260 result = g_new0 (data_object, 1);
1261
1262 result->ido.lpVtbl = &ido_vtbl;
1263 result->ref_count = 1;
1264 result->context = context;
1265
1266 GDK_NOTE (DND, g_print ("data_object_new: %p\n", result));
1267
1268 return result;
1269 }
1270
1271 static enum_formats *
1272 enum_formats_new (void)
1273 {
1274 enum_formats *result;
1275
1276 result = g_new0 (enum_formats, 1);
1277
1278 result->ief.lpVtbl = &ief_vtbl;
1279 result->ref_count = 1;
1280 result->ix = 0;
1281
1282 return result;
1283 }
1284
1285 void
1286 _gdk_win32_ole2_dnd_property_change (GdkAtom type,
1287 gint format,
1288 const guchar *data,
1289 gint nelements)
1290 {
1291 if (use_ole2_dnd)
1292 {
1293 HGLOBAL hdata = NULL;
1294
1295 if (active_pFormatEtc == NULL || active_pMedium == NULL)
1296 return;
1297
1298 /* Set up the data buffer for wide character text request */
1299 if (active_pFormatEtc->cfFormat == CF_UNICODETEXT)
1300 {
1301 gunichar2 *wdata;
1302 glong wlen;
1303
1304 wdata = g_utf8_to_utf16 ((const char *) data, -1, NULL, &wlen, NULL);
1305 hdata = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, (wlen + 1) * 2);
1306 if (hdata)
1307 {
1308 wchar_t *ptr = (wchar_t *) GlobalLock(hdata);
1309 memcpy (ptr, wdata, (wlen + 1) * 2);
1310 GlobalUnlock(hdata);
1311 }
1312 g_free (wdata);
1313 }
1314 else
1315 g_warning ("Only text handled for now");
1316
1317 /* Pack up data */
1318 active_pMedium->tymed = TYMED_HGLOBAL;
1319 active_pMedium->hGlobal = hdata;
1320 active_pMedium->pUnkForRelease = 0;
1321 }
1322 }
1323
1324 /* From MS Knowledge Base article Q130698 */
1325
1326 static gboolean
1327 resolve_link (HWND hWnd,
1328 wchar_t *link,
1329 gchar **lpszPath)
1330 {
1331 WIN32_FILE_ATTRIBUTE_DATA wfad;
1332 HRESULT hr;
1333 IShellLinkW *pslW = NULL;
1334 IPersistFile *ppf = NULL;
1335
1336 /* Check if the file is empty first because IShellLink::Resolve for
1337 * some reason succeeds with an empty file and returns an empty
1338 * "link target". (#524151)
1339 */
1340 if (!GetFileAttributesExW (link, GetFileExInfoStandard, &wfad) ||
1341 (wfad.nFileSizeHigh == 0 && wfad.nFileSizeLow == 0))
1342 return FALSE;
1343
1344 /* Assume failure to start with: */
1345 *lpszPath = 0;
1346
1347 /* Call CoCreateInstance to obtain the IShellLink interface
1348 * pointer. This call fails if CoInitialize is not called, so it is
1349 * assumed that CoInitialize has been called.
1350 */
1351
1352 hr = CoCreateInstance (&CLSID_ShellLink,
1353 NULL,
1354 CLSCTX_INPROC_SERVER,
1355 &IID_IShellLinkW,
1356 (LPVOID *)&pslW);
1357
1358 if (SUCCEEDED (hr))
1359 {
1360 /* The IShellLink interface supports the IPersistFile
1361 * interface. Get an interface pointer to it.
1362 */
1363 hr = pslW->lpVtbl->QueryInterface (pslW,
1364 &IID_IPersistFile,
1365 (LPVOID *) &ppf);
1366 }
1367
1368 if (SUCCEEDED (hr))
1369 {
1370 /* Load the file. */
1371 hr = ppf->lpVtbl->Load (ppf, link, STGM_READ);
1372 }
1373
1374 if (SUCCEEDED (hr))
1375 {
1376 /* Resolve the link by calling the Resolve()
1377 * interface function.
1378 */
1379 hr = pslW->lpVtbl->Resolve (pslW, hWnd, SLR_ANY_MATCH | SLR_NO_UI);
1380 }
1381
1382 if (SUCCEEDED (hr))
1383 {
1384 wchar_t wtarget[MAX_PATH];
1385
1386 hr = pslW->lpVtbl->GetPath (pslW, wtarget, MAX_PATH, NULL, 0);
1387 if (SUCCEEDED (hr))
1388 *lpszPath = g_utf16_to_utf8 (wtarget, -1, NULL, NULL, NULL);
1389 }
1390
1391 if (ppf)
1392 ppf->lpVtbl->Release (ppf);
1393
1394 if (pslW)
1395 pslW->lpVtbl->Release (pslW);
1396
1397 return SUCCEEDED (hr);
1398 }
1399
1400 #if 0
1401
1402 /* Check for filenames like C:\Users\tml\AppData\Local\Temp\d5qtkvvs.bmp */
1403 static gboolean
1404 filename_looks_tempish (const char *filename)
1405 {
1406 char *dirname;
1407 char *p;
1408 const char *q;
1409 gboolean retval = FALSE;
1410
1411 dirname = g_path_get_dirname (filename);
1412
1413 p = dirname;
1414 q = g_get_tmp_dir ();
1415
1416 while (*p && *q &&
1417 ((G_IS_DIR_SEPARATOR (*p) && G_IS_DIR_SEPARATOR (*q)) ||
1418 g_ascii_tolower (*p) == g_ascii_tolower (*q)))
1419 p++, q++;
1420
1421 if (!*p && !*q)
1422 retval = TRUE;
1423
1424 g_free (dirname);
1425
1426 return retval;
1427 }
1428
1429 static gboolean
1430 close_it (gpointer data)
1431 {
1432 close (GPOINTER_TO_INT (data));
1433
1434 return FALSE;
1435 }
1436
1437 #endif
1438
1439 static GdkFilterReturn
1440 gdk_dropfiles_filter (GdkXEvent *xev,
1441 GdkEvent *event,
1442 gpointer data)
1443 {
1444 GdkDragContext *context;
1445 GString *result;
1446 MSG *msg = (MSG *) xev;
1447 HANDLE hdrop;
1448 POINT pt;
1449 gint nfiles, i;
1450 gchar *fileName, *linkedFile;
1451
1452 if (msg->message == WM_DROPFILES)
1453 {
1454 GDK_NOTE (DND, g_print ("WM_DROPFILES: %p\n", msg->hwnd));
1455
1456 context = gdk_drag_context_new ();
1457 context->protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
1458 context->is_source = FALSE;
1459
1460 context->source_window = _gdk_root;
1461 g_object_ref (context->source_window);
1462
1463 context->dest_window = event->any.window;
1464 g_object_ref (context->dest_window);
1465
1466 /* WM_DROPFILES drops are always file names */
1467 context->targets =
1468 g_list_append (NULL, _text_uri_list);
1469 context->actions = GDK_ACTION_COPY;
1470 context->suggested_action = GDK_ACTION_COPY;
1471 current_dest_drag = context;
1472
1473 event->dnd.type = GDK_DROP_START;
1474 event->dnd.context = current_dest_drag;
1475
1476 hdrop = (HANDLE) msg->wParam;
1477 DragQueryPoint (hdrop, &pt);
1478 ClientToScreen (msg->hwnd, &pt);
1479
1480 event->dnd.x_root = pt.x + _gdk_offset_x;
1481 event->dnd.y_root = pt.y + _gdk_offset_y;
1482 event->dnd.time = _gdk_win32_get_next_tick (msg->time);
1483
1484 nfiles = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
1485
1486 result = g_string_new (NULL);
1487 for (i = 0; i < nfiles; i++)
1488 {
1489 gchar *uri;
1490 wchar_t wfn[MAX_PATH];
1491
1492 DragQueryFileW (hdrop, i, wfn, MAX_PATH);
1493 fileName = g_utf16_to_utf8 (wfn, -1, NULL, NULL, NULL);
1494
1495 /* Resolve shortcuts */
1496 if (resolve_link (msg->hwnd, wfn, &linkedFile))
1497 {
1498 uri = g_filename_to_uri (linkedFile, NULL, NULL);
1499 if (uri != NULL)
1500 {
1501 g_string_append (result, uri);
1502 GDK_NOTE (DND, g_print ("... %s link to %s: %s\n",
1503 fileName, linkedFile, uri));
1504 g_free (uri);
1505 }
1506 g_free (fileName);
1507 fileName = linkedFile;
1508 }
1509 else
1510 {
1511 uri = g_filename_to_uri (fileName, NULL, NULL);
1512 if (uri != NULL)
1513 {
1514 g_string_append (result, uri);
1515 GDK_NOTE (DND, g_print ("... %s: %s\n", fileName, uri));
1516 g_free (uri);
1517 }
1518 }
1519
1520 #if 0
1521 /* Awful hack to recognize temp files corresponding to
1522 * images dragged from Firefox... Open the file right here
1523 * so that it is less likely that Firefox manages to delete
1524 * it before the GTK+-using app (typically GIMP) has opened
1525 * it.
1526 *
1527 * Not compiled in for now, because it means images dragged
1528 * from Firefox would stay around in the temp folder which
1529 * is not what Firefox intended. I don't feel comfortable
1530 * with that, both from a geenral sanity point of view, and
1531 * from a privacy point of view. It's better to wait for
1532 * Firefox to fix the problem, for instance by deleting the
1533 * temp file after a longer delay, or to wait until we
1534 * implement the OLE2_DND...
1535 */
1536 if (filename_looks_tempish (fileName))
1537 {
1538 int fd = g_open (fileName, _O_RDONLY|_O_BINARY, 0);
1539 if (fd == -1)
1540 {
1541 GDK_NOTE (DND, g_print ("Could not open %s, maybe an image dragged from Firefox that it already deleted\n", fileName));
1542 }
1543 else
1544 {
1545 GDK_NOTE (DND, g_print ("Opened %s as %d so that Firefox won't delete it\n", fileName, fd));
1546 g_timeout_add_seconds (1, close_it, GINT_TO_POINTER (fd));
1547 }
1548 }
1549 #endif
1550
1551 g_free (fileName);
1552 g_string_append (result, "\015\012");
1553 }
1554 _gdk_dropfiles_store (result->str);
1555 g_string_free (result, FALSE);
1556
1557 DragFinish (hdrop);
1558
1559 return GDK_FILTER_TRANSLATE;
1560 }
1561 else
1562 return GDK_FILTER_CONTINUE;
1563 }
1564
1565 static void
1566 add_format (GArray *fmts,
1567 CLIPFORMAT cf)
1568 {
1569 FORMATETC fmt;
1570
1571 fmt.cfFormat = cf;
1572 fmt.ptd = NULL;
1573 fmt.dwAspect = DVASPECT_CONTENT;
1574 fmt.lindex = -1;
1575 fmt.tymed = TYMED_HGLOBAL;
1576
1577 g_array_append_val (fmts, fmt);
1578 }
1579
1580
1581 void
1582 _gdk_dnd_init (void)
1583 {
1584 if (getenv ("GDK_WIN32_USE_EXPERIMENTAL_OLE2_DND"))
1585 use_ole2_dnd = TRUE;
1586
1587 if (use_ole2_dnd)
1588 {
1589 HRESULT hr;
1590 GArray *fmts;
1591
1592 hr = OleInitialize (NULL);
1593
1594 if (! SUCCEEDED (hr))
1595 g_error ("OleInitialize failed");
1596
1597 fmts = g_array_new (FALSE, FALSE, sizeof (FORMATETC));
1598
1599 /* The most important presumably */
1600 add_format (fmts, CF_UNICODETEXT);
1601
1602 /* Used for GTK+ internal DND, I think was the intent? Anyway, code below assumes
1603 * this is at index 1.
1604 */
1605 add_format (fmts, CF_GDIOBJFIRST);
1606
1607 add_format (fmts, CF_HDROP);
1608
1609 add_format (fmts, _cf_png);
1610 add_format (fmts, CF_DIB);
1611
1612 add_format (fmts, _cf_url);
1613 add_format (fmts, _cf_html_format);
1614 add_format (fmts, _cf_text_html);
1615
1616 nformats = fmts->len;
1617 formats = (FORMATETC*) g_array_free (fmts, FALSE);
1618
1619 target_ctx_for_window = g_hash_table_new (g_direct_hash, g_direct_equal);
1620 }
1621 }
1622
1623 void
1624 _gdk_win32_dnd_exit (void)
1625 {
1626 if (use_ole2_dnd)
1627 {
1628 OleUninitialize ();
1629 }
1630 }
1631
1632 /* Source side */
1633
1634 static void
1635 local_send_leave (GdkDragContext *context,
1636 guint32 time)
1637 {
1638 GdkEvent tmp_event;
1639
1640 GDK_NOTE (DND, g_print ("local_send_leave: context=%p current_dest_drag=%p\n",
1641 context,
1642 current_dest_drag));
1643
1644 if ((current_dest_drag != NULL) &&
1645 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1646 (current_dest_drag->source_window == context->source_window))
1647 {
1648 tmp_event.dnd.type = GDK_DRAG_LEAVE;
1649 tmp_event.dnd.window = context->dest_window;
1650 /* Pass ownership of context to the event */
1651 tmp_event.dnd.send_event = FALSE;
1652 tmp_event.dnd.context = current_dest_drag;
1653 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1654
1655 current_dest_drag = NULL;
1656
1657 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
1658 gdk_event_put (&tmp_event);
1659 }
1660 }
1661
1662 static void
1663 local_send_enter (GdkDragContext *context,
1664 guint32 time)
1665 {
1666 GdkEvent tmp_event;
1667 GdkDragContextPrivateWin32 *private;
1668 GdkDragContext *new_context;
1669
1670 GDK_NOTE (DND, g_print ("local_send_enter: context=%p current_dest_drag=%p\n",
1671 context,
1672 current_dest_drag));
1673
1674 private = PRIVATE_DATA (context);
1675
1676 if (current_dest_drag != NULL)
1677 {
1678 g_object_unref (G_OBJECT (current_dest_drag));
1679 current_dest_drag = NULL;
1680 }
1681
1682 new_context = gdk_drag_context_new ();
1683 new_context->protocol = GDK_DRAG_PROTO_LOCAL;
1684 new_context->is_source = FALSE;
1685
1686 new_context->source_window = context->source_window;
1687 g_object_ref (new_context->source_window);
1688
1689 new_context->dest_window = context->dest_window;
1690 g_object_ref (new_context->dest_window);
1691
1692 new_context->targets = g_list_copy (context->targets);
1693
1694 gdk_window_set_events (new_context->source_window,
1695 gdk_window_get_events (new_context->source_window) |
1696 GDK_PROPERTY_CHANGE_MASK);
1697 new_context->actions = context->actions;
1698
1699 tmp_event.type = GDK_DRAG_ENTER;
1700 tmp_event.dnd.window = context->dest_window;
1701 tmp_event.dnd.send_event = FALSE;
1702 tmp_event.dnd.context = new_context;
1703 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1704
1705 current_dest_drag = new_context;
1706
1707 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
1708 gdk_event_put (&tmp_event);
1709 }
1710
1711 static void
1712 local_send_motion (GdkDragContext *context,
1713 gint x_root,
1714 gint y_root,
1715 GdkDragAction action,
1716 guint32 time)
1717 {
1718 GdkEvent tmp_event;
1719
1720 GDK_NOTE (DND, g_print ("local_send_motion: context=%p (%d,%d) current_dest_drag=%p\n",
1721 context, x_root, y_root,
1722 current_dest_drag));
1723
1724 if ((current_dest_drag != NULL) &&
1725 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1726 (current_dest_drag->source_window == context->source_window))
1727 {
1728 tmp_event.type = GDK_DRAG_MOTION;
1729 tmp_event.dnd.window = current_dest_drag->dest_window;
1730 tmp_event.dnd.send_event = FALSE;
1731 tmp_event.dnd.context = current_dest_drag;
1732 tmp_event.dnd.time = time;
1733
1734 current_dest_drag->suggested_action = action;
1735
1736 tmp_event.dnd.x_root = x_root;
1737 tmp_event.dnd.y_root = y_root;
1738
1739 PRIVATE_DATA (current_dest_drag)->last_pt.x = x_root - _gdk_offset_x;
1740 PRIVATE_DATA (current_dest_drag)->last_pt.y = y_root - _gdk_offset_y;
1741
1742 PRIVATE_DATA (context)->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1743
1744 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
1745 gdk_event_put (&tmp_event);
1746 }
1747 }
1748
1749 static void
1750 local_send_drop (GdkDragContext *context,
1751 guint32 time)
1752 {
1753 GdkEvent tmp_event;
1754
1755 GDK_NOTE (DND, g_print ("local_send_drop: context=%p current_dest_drag=%p\n",
1756 context,
1757 current_dest_drag));
1758
1759 if ((current_dest_drag != NULL) &&
1760 (current_dest_drag->protocol == GDK_DRAG_PROTO_LOCAL) &&
1761 (current_dest_drag->source_window == context->source_window))
1762 {
1763 GdkDragContextPrivateWin32 *private;
1764 private = PRIVATE_DATA (current_dest_drag);
1765
1766 /* Pass ownership of context to the event */
1767 tmp_event.type = GDK_DROP_START;
1768 tmp_event.dnd.window = current_dest_drag->dest_window;
1769 tmp_event.dnd.send_event = FALSE;
1770 tmp_event.dnd.context = current_dest_drag;
1771 tmp_event.dnd.time = GDK_CURRENT_TIME;
1772
1773 tmp_event.dnd.x_root = private->last_pt.x + _gdk_offset_x;
1774 tmp_event.dnd.y_root = private->last_pt.y + _gdk_offset_y;
1775
1776 current_dest_drag = NULL;
1777
1778 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
1779 gdk_event_put (&tmp_event);
1780 }
1781
1782 }
1783
1784 static void
1785 gdk_drag_do_leave (GdkDragContext *context,
1786 guint32 time)
1787 {
1788 if (context->dest_window)
1789 {
1790 GDK_NOTE (DND, g_print ("gdk_drag_do_leave\n"));
1791
1792 if (!use_ole2_dnd)
1793 {
1794 if (context->protocol == GDK_DRAG_PROTO_LOCAL)
1795 local_send_leave (context, time);
1796 }
1797
1798 g_object_unref (context->dest_window);
1799 context->dest_window = NULL;
1800 }
1801 }
1802
1803 GdkDragContext *
1804 gdk_drag_begin (GdkWindow *window,
1805 GList *targets)
1806 {
1807 if (!use_ole2_dnd)
1808 {
1809 GdkDragContext *new_context;
1810
1811 g_return_val_if_fail (window != NULL, NULL);
1812
1813 new_context = gdk_drag_context_new ();
1814
1815 new_context->is_source = TRUE;
1816
1817 new_context->source_window = window;
1818 g_object_ref (window);
1819
1820 new_context->targets = g_list_copy (targets);
1821 new_context->actions = 0;
1822
1823 return new_context;
1824 }
1825 else
1826 {
1827 source_drag_context *ctx;
1828
1829 g_return_val_if_fail (window != NULL, NULL);
1830
1831 GDK_NOTE (DND, g_print ("gdk_drag_begin\n"));
1832
1833 ctx = source_context_new (window, targets);
1834
1835 _dnd_source_state = GDK_WIN32_DND_PENDING;
1836
1837 pending_src_context = ctx;
1838 g_object_ref (ctx->context);
1839
1840 return ctx->context;
1841 }
1842 }
1843
1844 void
1845 _gdk_win32_dnd_do_dragdrop (void)
1846 {
1847 if (use_ole2_dnd)
1848 {
1849 GdkDragContext* drag_ctx;
1850 GdkDragContextPrivateWin32 *private;
1851 BYTE kbd_state[256];
1852 data_object *dobj;
1853 HRESULT hr;
1854 DWORD dwEffect;
1855 #if 0
1856 HGLOBAL global;
1857 STGMEDIUM medium;
1858 #endif
1859
1860 if (pending_src_context == NULL)
1861 return;
1862
1863 drag_ctx = pending_src_context->context;
1864 private = PRIVATE_DATA (drag_ctx);
1865
1866 dobj = data_object_new (drag_ctx);
1867
1868 API_CALL (GetCursorPos, (&private->last_pt));
1869 API_CALL (ScreenToClient, (GDK_WINDOW_HWND (drag_ctx->source_window), &private->last_pt));
1870 private->last_key_state = 0;
1871 API_CALL (GetKeyboardState, (kbd_state));
1872
1873 if (kbd_state[VK_CONTROL])
1874 private->last_key_state |= MK_CONTROL;
1875 if (kbd_state[VK_SHIFT])
1876 private->last_key_state |= MK_SHIFT;
1877 if (kbd_state[VK_LBUTTON])
1878 private->last_key_state |= MK_LBUTTON;
1879 if (kbd_state[VK_MBUTTON])
1880 private->last_key_state |= MK_MBUTTON;
1881 if (kbd_state[VK_RBUTTON])
1882 private->last_key_state |= MK_RBUTTON;
1883
1884 #if 0
1885 global = GlobalAlloc (GMEM_FIXED, sizeof (ctx));
1886
1887 memcpy (&global, ctx, sizeof (ctx));
1888
1889 medium.tymed = TYMED_HGLOBAL;
1890 medium.hGlobal = global;
1891 medium.pUnkForRelease = NULL;
1892
1893 /* FIXME I wish I remember what I was thinking of here, i.e. what
1894 * the formats[1] signifies, i.e. the CF_GDIOBJFIRST FORMATETC?
1895 */
1896 dobj->ido.lpVtbl->SetData (&dobj->ido, &formats[1], &medium, TRUE);
1897 #endif
1898
1899 /* Start dragging with mainloop inside the OLE2 API. Exits only when done */
1900
1901 GDK_NOTE (DND, g_print ("Calling DoDragDrop\n"));
1902
1903 _gdk_win32_begin_modal_call ();
1904 hr = DoDragDrop (&dobj->ido, &pending_src_context->ids,
1905 DROPEFFECT_COPY | DROPEFFECT_MOVE,
1906 &dwEffect);
1907 _gdk_win32_end_modal_call ();
1908
1909 GDK_NOTE (DND, g_print ("DoDragDrop returned %s\n",
1910 (hr == DRAGDROP_S_DROP ? "DRAGDROP_S_DROP" :
1911 (hr == DRAGDROP_S_CANCEL ? "DRAGDROP_S_CANCEL" :
1912 (hr == E_UNEXPECTED ? "E_UNEXPECTED" :
1913 g_strdup_printf ("%#.8lx", hr))))));
1914
1915 /* Delete dnd selection after successful move */
1916 if (hr == DRAGDROP_S_DROP && dwEffect == DROPEFFECT_MOVE)
1917 {
1918 GdkEvent tmp_event;
1919
1920 tmp_event.type = GDK_SELECTION_REQUEST;
1921 tmp_event.selection.window = drag_ctx->source_window;
1922 tmp_event.selection.send_event = FALSE;
1923 tmp_event.selection.selection = _gdk_ole2_dnd;
1924 tmp_event.selection.target = _delete;
1925 tmp_event.selection.property = _gdk_ole2_dnd; /* ??? */
1926 tmp_event.selection.time = GDK_CURRENT_TIME; /* ??? */
1927 g_object_ref (tmp_event.selection.window);
1928
1929 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
1930 gdk_event_put (&tmp_event);
1931 }
1932
1933 #if 0
1934 // Send a GDK_DROP_FINISHED to the source window
1935 GetCursorPos (&pt);
1936 ptl.x = pt.x;
1937 ptl.y = pt.y;
1938 if ( pending_src_context != NULL && pending_src_context->context != NULL
1939 && pending_src_context->context->source_window != NULL )
1940 push_dnd_event (GDK_DROP_FINISHED, pending_src_context->context, ptl, FALSE);
1941 #endif
1942
1943 dobj->ido.lpVtbl->Release (&dobj->ido);
1944 if (pending_src_context != NULL)
1945 {
1946 pending_src_context->ids.lpVtbl->Release (&pending_src_context->ids);
1947 pending_src_context = NULL;
1948 }
1949 }
1950 }
1951
1952 GdkNativeWindow
1953 gdk_drag_get_protocol_for_display (GdkDisplay *display,
1954 GdkNativeWindow xid,
1955 GdkDragProtocol *protocol)
1956 {
1957 GdkWindow *window;
1958
1959 window = gdk_window_lookup (xid);
1960 if (window &&
1961 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
1962 {
1963 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
1964 {
1965 if (use_ole2_dnd)
1966 *protocol = GDK_DRAG_PROTO_OLE2;
1967 else
1968 *protocol = GDK_DRAG_PROTO_LOCAL;
1969
1970 return xid;
1971 }
1972 }
1973
1974 return 0;
1975 }
1976
1977 typedef struct {
1978 gint x;
1979 gint y;
1980 HWND ignore;
1981 HWND result;
1982 } find_window_enum_arg;
1983
1984 static BOOL CALLBACK
1985 find_window_enum_proc (HWND hwnd,
1986 LPARAM lparam)
1987 {
1988 RECT rect;
1989 POINT tl, br;
1990 find_window_enum_arg *a = (find_window_enum_arg *) lparam;
1991
1992 if (hwnd == a->ignore)
1993 return TRUE;
1994
1995 if (!IsWindowVisible (hwnd))
1996 return TRUE;
1997
1998 tl.x = tl.y = 0;
1999 ClientToScreen (hwnd, &tl);
2000 GetClientRect (hwnd, &rect);
2001 br.x = rect.right;
2002 br.y = rect.bottom;
2003 ClientToScreen (hwnd, &br);
2004
2005 if (a->x >= tl.x && a->y >= tl.y && a->x < br.x && a->y < br.y)
2006 {
2007 a->result = hwnd;
2008 return FALSE;
2009 }
2010 else
2011 return TRUE;
2012 }
2013
2014 void
2015 gdk_drag_find_window_for_screen (GdkDragContext *context,
2016 GdkWindow *drag_window,
2017 GdkScreen *screen,
2018 gint x_root,
2019 gint y_root,
2020 GdkWindow **dest_window,
2021 GdkDragProtocol *protocol)
2022 {
2023 GdkWindow *dw;
2024 find_window_enum_arg a;
2025
2026 a.x = x_root - _gdk_offset_x;
2027 a.y = y_root - _gdk_offset_y;
2028 a.ignore = drag_window ? GDK_WINDOW_HWND (drag_window) : NULL;
2029 a.result = NULL;
2030
2031 EnumWindows (find_window_enum_proc, (LPARAM) &a);
2032
2033 if (a.result == NULL)
2034 *dest_window = NULL;
2035 else
2036 {
2037 dw = gdk_win32_handle_table_lookup (a.result);
2038 if (dw)
2039 {
2040 *dest_window = gdk_window_get_toplevel (dw);
2041 g_object_ref (*dest_window);
2042 }
2043 else
2044 *dest_window = gdk_window_foreign_new_for_display (_gdk_display, a.result);
2045
2046 if (use_ole2_dnd)
2047 *protocol = GDK_DRAG_PROTO_OLE2;
2048 else if (context->source_window)
2049 *protocol = GDK_DRAG_PROTO_LOCAL;
2050 else
2051 *protocol = GDK_DRAG_PROTO_WIN32_DROPFILES;
2052 }
2053
2054 GDK_NOTE (DND,
2055 g_print ("gdk_drag_find_window: %p %+d%+d: %p: %p %s\n",
2056 (drag_window ? GDK_WINDOW_HWND (drag_window) : NULL),
2057 x_root, y_root,
2058 a.result,
2059 (*dest_window ? GDK_WINDOW_HWND (*dest_window) : NULL),
2060 _gdk_win32_drag_protocol_to_string (*protocol)));
2061 }
2062
2063 gboolean
2064 gdk_drag_motion (GdkDragContext *context,
2065 GdkWindow *dest_window,
2066 GdkDragProtocol protocol,
2067 gint x_root,
2068 gint y_root,
2069 GdkDragAction suggested_action,
2070 GdkDragAction possible_actions,
2071 guint32 time)
2072 {
2073 GdkDragContextPrivateWin32 *private;
2074
2075 g_return_val_if_fail (context != NULL, FALSE);
2076
2077 context->actions = possible_actions;
2078
2079 GDK_NOTE (DND, g_print ("gdk_drag_motion: %s suggested=%s, possible=%s\n"
2080 " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2081 _gdk_win32_drag_protocol_to_string (protocol),
2082 _gdk_win32_drag_action_to_string (suggested_action),
2083 _gdk_win32_drag_action_to_string (possible_actions),
2084 context,
2085 _gdk_win32_drag_action_to_string (context->actions),
2086 _gdk_win32_drag_action_to_string (context->suggested_action),
2087 _gdk_win32_drag_action_to_string (context->action)));
2088
2089 private = PRIVATE_DATA (context);
2090
2091 if (!use_ole2_dnd)
2092 {
2093 if (context->dest_window == dest_window)
2094 {
2095 GdkDragContext *dest_context;
2096
2097 dest_context = gdk_drag_context_find (FALSE,
2098 context->source_window,
2099 dest_window);
2100
2101 if (dest_context)
2102 dest_context->actions = context->actions;
2103
2104 context->suggested_action = suggested_action;
2105 }
2106 else
2107 {
2108 GdkEvent tmp_event;
2109
2110 /* Send a leave to the last destination */
2111 gdk_drag_do_leave (context, time);
2112 private->drag_status = GDK_DRAG_STATUS_DRAG;
2113
2114 /* Check if new destination accepts drags, and which protocol */
2115 if (dest_window)
2116 {
2117 context->dest_window = dest_window;
2118 g_object_ref (context->dest_window);
2119 context->protocol = protocol;
2120
2121 switch (protocol)
2122 {
2123 case GDK_DRAG_PROTO_LOCAL:
2124 local_send_enter (context, time);
2125 break;
2126
2127 default:
2128 break;
2129 }
2130 context->suggested_action = suggested_action;
2131 }
2132 else
2133 {
2134 context->dest_window = NULL;
2135 context->action = 0;
2136 }
2137
2138 /* Push a status event, to let the client know that
2139 * the drag changed
2140 */
2141 tmp_event.type = GDK_DRAG_STATUS;
2142 tmp_event.dnd.window = context->source_window;
2143 /* We use this to signal a synthetic status. Perhaps
2144 * we should use an extra field...
2145 */
2146 tmp_event.dnd.send_event = TRUE;
2147
2148 tmp_event.dnd.context = context;
2149 tmp_event.dnd.time = time;
2150
2151 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
2152 gdk_event_put (&tmp_event);
2153 }
2154
2155 /* Send a drag-motion event */
2156
2157 private->last_pt.x = x_root - _gdk_offset_x;
2158 private->last_pt.y = y_root - _gdk_offset_y;
2159
2160 if (context->dest_window)
2161 {
2162 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2163 {
2164 switch (context->protocol)
2165 {
2166 case GDK_DRAG_PROTO_LOCAL:
2167 local_send_motion (context, x_root, y_root, suggested_action, time);
2168 break;
2169
2170 case GDK_DRAG_PROTO_NONE:
2171 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
2172 break;
2173
2174 default:
2175 break;
2176 }
2177 }
2178 else
2179 {
2180 GDK_NOTE (DND, g_print (" returning TRUE\n"
2181 " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2182 context,
2183 _gdk_win32_drag_action_to_string (context->actions),
2184 _gdk_win32_drag_action_to_string (context->suggested_action),
2185 _gdk_win32_drag_action_to_string (context->action)));
2186 return TRUE;
2187 }
2188 }
2189 }
2190
2191 GDK_NOTE (DND, g_print (" returning FALSE\n"
2192 " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2193 context,
2194 _gdk_win32_drag_action_to_string (context->actions),
2195 _gdk_win32_drag_action_to_string (context->suggested_action),
2196 _gdk_win32_drag_action_to_string (context->action)));
2197 return FALSE;
2198 }
2199
2200 void
2201 gdk_drag_drop (GdkDragContext *context,
2202 guint32 time)
2203 {
2204 g_return_if_fail (context != NULL);
2205
2206 GDK_NOTE (DND, g_print ("gdk_drag_drop\n"));
2207
2208 if (!use_ole2_dnd)
2209 {
2210 if (context->dest_window &&
2211 context->protocol == GDK_DRAG_PROTO_LOCAL)
2212 local_send_drop (context, time);
2213 }
2214 else
2215 {
2216 _dnd_source_state = GDK_WIN32_DND_DROPPED;
2217 }
2218 }
2219
2220 void
2221 gdk_drag_abort (GdkDragContext *context,
2222 guint32 time)
2223 {
2224 g_return_if_fail (context != NULL);
2225
2226 GDK_NOTE (DND, g_print ("gdk_drag_abort\n"));
2227
2228 if (use_ole2_dnd)
2229 _dnd_source_state = GDK_WIN32_DND_NONE;
2230 }
2231
2232 /* Destination side */
2233
2234 void
2235 gdk_drag_status (GdkDragContext *context,
2236 GdkDragAction action,
2237 guint32 time)
2238 {
2239 GdkDragContextPrivateWin32 *private;
2240 GdkDragContext *src_context;
2241 GdkEvent tmp_event;
2242
2243 g_return_if_fail (context != NULL);
2244
2245 private = PRIVATE_DATA (context);
2246
2247 GDK_NOTE (DND, g_print ("gdk_drag_status: %s\n"
2248 " context=%p:{actions=%s,suggested=%s,action=%s}\n",
2249 _gdk_win32_drag_action_to_string (action),
2250 context,
2251 _gdk_win32_drag_action_to_string (context->actions),
2252 _gdk_win32_drag_action_to_string (context->suggested_action),
2253 _gdk_win32_drag_action_to_string (context->action)));
2254
2255 context->action = action;
2256
2257 if (!use_ole2_dnd)
2258 {
2259 src_context = gdk_drag_context_find (TRUE,
2260 context->source_window,
2261 context->dest_window);
2262
2263 if (src_context)
2264 {
2265 GdkDragContextPrivateWin32 *private = PRIVATE_DATA (src_context);
2266
2267 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2268 private->drag_status = GDK_DRAG_STATUS_DRAG;
2269
2270 tmp_event.type = GDK_DRAG_STATUS;
2271 tmp_event.dnd.window = context->source_window;
2272 tmp_event.dnd.send_event = FALSE;
2273 tmp_event.dnd.context = src_context;
2274 tmp_event.dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2275
2276 if (action == GDK_ACTION_DEFAULT)
2277 action = 0;
2278
2279 src_context->action = action;
2280
2281 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
2282 gdk_event_put (&tmp_event);
2283 }
2284 }
2285 }
2286
2287 void
2288 gdk_drop_reply (GdkDragContext *context,
2289 gboolean ok,
2290 guint32 time)
2291 {
2292 g_return_if_fail (context != NULL);
2293
2294 GDK_NOTE (DND, g_print ("gdk_drop_reply\n"));
2295
2296 if (!use_ole2_dnd)
2297 if (context->dest_window)
2298 {
2299 if (context->protocol == GDK_DRAG_PROTO_WIN32_DROPFILES)
2300 _gdk_dropfiles_store (NULL);
2301 }
2302 }
2303
2304 void
2305 gdk_drop_finish (GdkDragContext *context,
2306 gboolean success,
2307 guint32 time)
2308 {
2309 GdkDragContextPrivateWin32 *private;
2310 GdkDragContext *src_context;
2311 GdkEvent tmp_event;
2312
2313 g_return_if_fail (context != NULL);
2314
2315 GDK_NOTE (DND, g_print ("gdk_drop_finish\n"));
2316
2317 private = PRIVATE_DATA (context);
2318
2319 if (!use_ole2_dnd)
2320 {
2321 src_context = gdk_drag_context_find (TRUE,
2322 context->source_window,
2323 context->dest_window);
2324 if (src_context)
2325 {
2326 tmp_event.type = GDK_DROP_FINISHED;
2327 tmp_event.dnd.window = src_context->source_window;
2328 tmp_event.dnd.send_event = FALSE;
2329 tmp_event.dnd.context = src_context;
2330
2331 GDK_NOTE (EVENTS, _gdk_win32_print_event (&tmp_event));
2332 gdk_event_put (&tmp_event);
2333 }
2334 }
2335 else
2336 {
2337 gdk_drag_do_leave (context, time);
2338
2339 if (success)
2340 _dnd_target_state = GDK_WIN32_DND_DROPPED;
2341 else
2342 _dnd_target_state = GDK_WIN32_DND_FAILED;
2343 }
2344 }
2345
2346 #if 0
2347
2348 static GdkFilterReturn
2349 gdk_destroy_filter (GdkXEvent *xev,
2350 GdkEvent *event,
2351 gpointer data)
2352 {
2353 MSG *msg = (MSG *) xev;
2354
2355 if (msg->message == WM_DESTROY)
2356 {
2357 IDropTarget *idtp = (IDropTarget *) data;
2358
2359 GDK_NOTE (DND, g_print ("gdk_destroy_filter: WM_DESTROY: %p\n", msg->hwnd));
2360 #if 0
2361 idtp->lpVtbl->Release (idtp);
2362 #endif
2363 RevokeDragDrop (msg->hwnd);
2364 CoLockObjectExternal ((IUnknown*) idtp, FALSE, TRUE);
2365 }
2366 return GDK_FILTER_CONTINUE;
2367 }
2368
2369 #endif
2370
2371 void
2372 gdk_window_register_dnd (GdkWindow *window)
2373 {
2374 target_drag_context *ctx;
2375 HRESULT hr;
2376
2377 g_return_if_fail (window != NULL);
2378
2379 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
2380 return;
2381
2382 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2383 return;
2384 else
2385 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
2386
2387 GDK_NOTE (DND, g_print ("gdk_window_register_dnd: %p\n", GDK_WINDOW_HWND (window)));
2388
2389 if (!use_ole2_dnd)
2390 {
2391 /* We always claim to accept dropped files, but in fact we might not,
2392 * of course. This function is called in such a way that it cannot know
2393 * whether the window (widget) in question actually accepts files
2394 * (in gtk, data of type text/uri-list) or not.
2395 */
2396 gdk_window_add_filter (window, gdk_dropfiles_filter, NULL);
2397 DragAcceptFiles (GDK_WINDOW_HWND (window), TRUE);
2398 }
2399 else
2400 {
2401 /* Return if window is already setup for DND. */
2402 if (g_hash_table_lookup (target_ctx_for_window, GDK_WINDOW_HWND (window)) != NULL)
2403 return;
2404
2405 /* Register for OLE2 d&d : similarly, claim to accept all supported
2406 * data types because we cannot know from here what the window
2407 * actually accepts.
2408 */
2409 /* FIXME: This of course won't work with user-extensible data types! */
2410 ctx = target_context_new (window);
2411
2412 hr = CoLockObjectExternal ((IUnknown *) &ctx->idt, TRUE, FALSE);
2413 if (!SUCCEEDED (hr))
2414 OTHER_API_FAILED ("CoLockObjectExternal");
2415 else
2416 {
2417 hr = RegisterDragDrop (GDK_WINDOW_HWND (window), &ctx->idt);
2418 if (hr == DRAGDROP_E_ALREADYREGISTERED)
2419 {
2420 g_print ("DRAGDROP_E_ALREADYREGISTERED\n");
2421 CoLockObjectExternal ((IUnknown *) &ctx->idt, FALSE, FALSE);
2422 }
2423 else if (!SUCCEEDED (hr))
2424 OTHER_API_FAILED ("RegisterDragDrop");
2425 else
2426 {
2427 gdk_window_ref (window);
2428 g_hash_table_insert (target_ctx_for_window, GDK_WINDOW_HWND (window), ctx);
2429 }
2430 }
2431 }
2432 }
2433
2434 GdkAtom
2435 gdk_drag_get_selection (GdkDragContext *context)
2436 {
2437 switch (context->protocol)
2438 {
2439 case GDK_DRAG_PROTO_LOCAL:
2440 return _local_dnd;
2441 case GDK_DRAG_PROTO_WIN32_DROPFILES:
2442 return _gdk_win32_dropfiles;
2443 case GDK_DRAG_PROTO_OLE2:
2444 return _gdk_ole2_dnd;
2445 default:
2446 return GDK_NONE;
2447 }
2448 }
2449
2450 gboolean
2451 gdk_drag_drop_succeeded (GdkDragContext *context)
2452 {
2453 GdkDragContextPrivateWin32 *private;
2454
2455 g_return_val_if_fail (context != NULL, FALSE);
2456
2457 private = PRIVATE_DATA (context);
2458
2459 /* FIXME: Can we set drop_failed when the drop has failed? */
2460 return !private->drop_failed;
2461 }