4 * Copyright 1995 Martin von Loewis
5 * Copyright 1999 Francis Beaudet
17 #include "wine/obj_base.h"
18 #include "wine/obj_clientserver.h"
19 #include "wine/obj_storage.h"
20 #include "wine/obj_moniker.h"
22 /******************************************************************************
23 * These are static/global variables and internal data structures that the
24 * OLE module uses to maintain it's state.
26 typedef struct tagDropTargetNode
29 IDropTarget
* dropTarget
;
30 struct tagDropTargetNode
* prevDropTarget
;
31 struct tagDropTargetNode
* nextDropTarget
;
34 typedef struct tagTrackerWindowInfo
36 IDataObject
* dataObject
;
37 IDropSource
* dropSource
;
44 HWND32 curDragTargetHWND
;
45 IDropTarget
* curDragTarget
;
49 * This is the lock count on the OLE library. It is controlled by the
50 * OLEInitialize/OLEUninitialize methods.
52 static ULONG OLE_moduleLockCount
= 0;
55 * Name of our registered window class.
57 static const char OLEDD_DRAGTRACKERCLASS
[] = "WineDragDropTracker32";
60 * This is the head of the Drop target container.
62 static DropTargetNode
* targetListHead
= NULL
;
64 /******************************************************************************
65 * These are the prototypes of the utility methods used for OLE Drag n Drop
67 static void OLEDD_Initialize();
68 static void OLEDD_UnInitialize();
69 static void OLEDD_InsertDropTarget(
70 DropTargetNode
* nodeToAdd
);
71 static DropTargetNode
* OLEDD_ExtractDropTarget(
73 static DropTargetNode
* OLEDD_FindDropTarget(
75 static LRESULT WINAPI
OLEDD_DragTrackerWindowProc(
80 static void OLEDD_TrackMouseMove(
81 TrackerWindowInfo
* trackerInfo
,
84 static void OLEDD_TrackStateChange(
85 TrackerWindowInfo
* trackerInfo
,
88 static DWORD
OLEDD_GetButtonState();
91 /******************************************************************************
92 * OleBuildVersion [OLE2.1]
94 DWORD WINAPI
OleBuildVersion(void)
96 TRACE(ole
,"(void)\n");
100 /***********************************************************************
101 * OleInitialize (OLE2.2) (OLE32.108)
103 HRESULT WINAPI
OleInitialize(LPVOID reserved
)
107 TRACE(ole
, "(%p)\n", reserved
);
110 * The first duty of the OleInitialize is to initialize the COM libraries.
112 hr
= CoInitializeEx32(NULL
, COINIT_APARTMENTTHREADED
);
115 * If the CoInitializeEx call failed, the OLE libraries can't be
122 * Then, it has to initialize the OLE specific modules.
126 * Object linking and Embedding
127 * In-place activation
129 if (OLE_moduleLockCount
==0)
132 * Initialize the libraries.
134 TRACE(ole
, "() - Initializing the OLE libraries\n");
143 * Then, we increase the lock count on the OLE module.
145 OLE_moduleLockCount
++;
150 /******************************************************************************
151 * CoGetCurrentProcess [COMPOBJ.34] [OLE2.2][OLE32.108]
154 * Is DWORD really the correct return type for this function?
156 DWORD WINAPI
CoGetCurrentProcess(void) {
157 return (DWORD
)PROCESS_Current();
160 /******************************************************************************
161 * OleUninitialize [OLE2.3] [OLE32.131]
163 void WINAPI
OleUninitialize(void)
168 * Decrease the lock count on the OLE module.
170 OLE_moduleLockCount
--;
173 * If we hit the bottom of the lock stack, free the libraries.
175 if (OLE_moduleLockCount
==0)
178 * Actually free the libraries.
180 TRACE(ole
, "() - Freeing the last reference count\n");
185 OLEDD_UnInitialize();
189 * Then, uninitialize the COM libraries.
194 /***********************************************************************
195 * OleFlushClipboard [OLE2.76]
197 HRESULT WINAPI
OleFlushClipboard(void)
202 /***********************************************************************
203 * OleSetClipboard [OLE32.127]
205 HRESULT WINAPI
OleSetClipboard(LPVOID pDataObj
)
207 FIXME(ole
,"(%p), stub!\n", pDataObj
);
211 /******************************************************************************
212 * CoRegisterMessageFilter32 [OLE32.38]
214 HRESULT WINAPI
CoRegisterMessageFilter32(
215 LPMESSAGEFILTER lpMessageFilter
, /* Pointer to interface */
216 LPMESSAGEFILTER
*lplpMessageFilter
/* Indirect pointer to prior instance if non-NULL */
219 if (lplpMessageFilter
) {
220 *lplpMessageFilter
= NULL
;
225 /******************************************************************************
226 * OleInitializeWOW [OLE32.109]
228 HRESULT WINAPI
OleInitializeWOW(DWORD x
) {
229 FIXME(ole
,"(0x%08lx),stub!\n",x
);
233 /***********************************************************************
234 * RegisterDragDrop16 (OLE2.35)
236 HRESULT WINAPI
RegisterDragDrop16(
238 LPDROPTARGET pDropTarget
240 FIXME(ole
,"(0x%04x,%p),stub!\n",hwnd
,pDropTarget
);
244 /***********************************************************************
245 * RegisterDragDrop32 (OLE32.139)
247 HRESULT WINAPI
RegisterDragDrop32(
249 LPDROPTARGET pDropTarget
)
251 DropTargetNode
* dropTargetInfo
;
253 TRACE(ole
,"(0x%x,%p)\n", hwnd
, pDropTarget
);
256 * First, check if the window is already registered.
258 dropTargetInfo
= OLEDD_FindDropTarget(hwnd
);
260 if (dropTargetInfo
!=NULL
)
261 return DRAGDROP_E_ALREADYREGISTERED
;
264 * If it's not there, we can add it. We first create a node for it.
266 dropTargetInfo
= HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode
));
268 if (dropTargetInfo
==NULL
)
269 return E_OUTOFMEMORY
;
271 dropTargetInfo
->hwndTarget
= hwnd
;
272 dropTargetInfo
->prevDropTarget
= NULL
;
273 dropTargetInfo
->nextDropTarget
= NULL
;
276 * Don't forget that this is an interface pointer, need to nail it down since
277 * we keep a copy of it.
279 dropTargetInfo
->dropTarget
= pDropTarget
;
280 IDropTarget_AddRef(dropTargetInfo
->dropTarget
);
282 OLEDD_InsertDropTarget(dropTargetInfo
);
287 /***********************************************************************
288 * RevokeDragDrop16 (OLE2.36)
290 HRESULT WINAPI
RevokeDragDrop16(
293 FIXME(ole
,"(0x%04x),stub!\n",hwnd
);
297 /***********************************************************************
298 * RevokeDragDrop32 (OLE32.141)
300 HRESULT WINAPI
RevokeDragDrop32(
303 DropTargetNode
* dropTargetInfo
;
305 TRACE(ole
,"(0x%x)\n", hwnd
);
308 * First, check if the window is already registered.
310 dropTargetInfo
= OLEDD_ExtractDropTarget(hwnd
);
313 * If it ain't in there, it's an error.
315 if (dropTargetInfo
==NULL
)
316 return DRAGDROP_E_NOTREGISTERED
;
319 * If it's in there, clean-up it's used memory and
322 IDropTarget_Release(dropTargetInfo
->dropTarget
);
323 HeapFree(GetProcessHeap(), 0, dropTargetInfo
);
328 /***********************************************************************
329 * OleRegGetUserType (OLE32.122)
331 HRESULT WINAPI
OleRegGetUserType32(
334 LPOLESTR32
* pszUserType
)
336 FIXME(ole
,",stub!\n");
340 /***********************************************************************
341 * DoDragDrop32 [OLE32.65]
343 HRESULT WINAPI
DoDragDrop32 (
344 IDataObject
*pDataObject
, /* ptr to the data obj */
345 IDropSource
* pDropSource
, /* ptr to the source obj */
346 DWORD dwOKEffect
, /* effects allowed by the source */
347 DWORD
*pdwEffect
) /* ptr to effects of the source */
349 TrackerWindowInfo trackerInfo
;
350 HWND32 hwndTrackWindow
;
353 TRACE(ole
,"(DataObject %p, DropSource %p)\n", pDataObject
, pDropSource
);
356 * Setup the drag n drop tracking window.
358 trackerInfo
.dataObject
= pDataObject
;
359 trackerInfo
.dropSource
= pDropSource
;
360 trackerInfo
.dwOKEffect
= dwOKEffect
;
361 trackerInfo
.pdwEffect
= pdwEffect
;
362 trackerInfo
.trackingDone
= FALSE
;
363 trackerInfo
.escPressed
= FALSE
;
364 trackerInfo
.curDragTargetHWND
= 0;
365 trackerInfo
.curDragTarget
= 0;
367 hwndTrackWindow
= CreateWindow32A(OLEDD_DRAGTRACKERCLASS
,
370 CW_USEDEFAULT32
, CW_USEDEFAULT32
,
371 CW_USEDEFAULT32
, CW_USEDEFAULT32
,
375 (LPVOID
)&trackerInfo
);
377 if (hwndTrackWindow
!=0)
380 * Capture the mouse input
382 SetCapture32(hwndTrackWindow
);
385 * Pump messages. All mouse input should go the the capture window.
387 while (!trackerInfo
.trackingDone
&& GetMessage32A(&msg
, 0, 0, 0) )
389 if ( (msg
.message
>= WM_KEYFIRST
) &&
390 (msg
.message
<= WM_KEYFIRST
) )
393 * When keyboard messages are sent to windows on this thread, we
394 * want to ignore notify the drop source that the state changed.
395 * in the case of the Escape key, we also notify the drop source
396 * we give it a special meaning.
398 if ( (msg
.message
==WM_KEYDOWN
) &&
399 (msg
.wParam
==VK_ESCAPE
) )
401 trackerInfo
.escPressed
= TRUE
;
405 * Notify the drop source.
407 OLEDD_TrackStateChange(&trackerInfo
,
409 OLEDD_GetButtonState());
414 * Dispatch the messages only when it's not a keyboard message.
416 DispatchMessage32A(&msg
);
421 * Destroy the temporary window.
423 DestroyWindow32(hwndTrackWindow
);
425 return trackerInfo
.returnValue
;
431 /***********************************************************************
432 * OleQueryLinkFromData32 [OLE32.118]
434 HRESULT WINAPI
OleQueryLinkFromData32(
435 IDataObject
* pSrcDataObject
)
437 FIXME(ole
,"(%p),stub!\n", pSrcDataObject
);
441 /***********************************************************************
442 * OleRegGetMiscStatus32 [OLE32.121]
444 HRESULT WINAPI
OleRegGetMiscStatus32(
449 FIXME(ole
,"(),stub!\n");
450 return REGDB_E_CLASSNOTREG
;
453 /***********************************************************************
454 * OleGetClipboard32 [OLE32.105]
456 HRESULT WINAPI
OleGetClipboard32(
457 IDataObject
** ppDataObj
)
459 FIXME(ole
,"(%p),stub!\n", ppDataObj
);
470 * Initializes the OLE drag and drop data structures.
472 static void OLEDD_Initialize()
474 WNDCLASS32A wndClass
;
476 ZeroMemory (&wndClass
, sizeof(WNDCLASS32A
));
477 wndClass
.style
= CS_GLOBALCLASS
;
478 wndClass
.lpfnWndProc
= (WNDPROC32
)OLEDD_DragTrackerWindowProc
;
479 wndClass
.cbClsExtra
= 0;
480 wndClass
.cbWndExtra
= sizeof(TrackerWindowInfo
*);
481 wndClass
.hCursor
= 0;
482 wndClass
.hbrBackground
= 0;
483 wndClass
.lpszClassName
= OLEDD_DRAGTRACKERCLASS
;
485 RegisterClass32A (&wndClass
);
489 * OLEDD_UnInitialize()
491 * Releases the OLE drag and drop data structures.
493 static void OLEDD_UnInitialize()
496 * Simply empty the list.
498 while (targetListHead
!=NULL
)
500 RevokeDragDrop32(targetListHead
->hwndTarget
);
505 * OLEDD_InsertDropTarget()
507 * Insert the target node in the tree.
509 static void OLEDD_InsertDropTarget(DropTargetNode
* nodeToAdd
)
511 DropTargetNode
* curNode
;
512 DropTargetNode
** parentNodeLink
;
515 * Iterate the tree to find the insertion point.
517 curNode
= targetListHead
;
518 parentNodeLink
= &targetListHead
;
520 while (curNode
!=NULL
)
522 if (nodeToAdd
->hwndTarget
<curNode
->hwndTarget
)
525 * If the node we want to add has a smaller HWND, go left
527 parentNodeLink
= &curNode
->prevDropTarget
;
528 curNode
= curNode
->prevDropTarget
;
530 else if (nodeToAdd
->hwndTarget
>curNode
->hwndTarget
)
533 * If the node we want to add has a larger HWND, go right
535 parentNodeLink
= &curNode
->nextDropTarget
;
536 curNode
= curNode
->nextDropTarget
;
541 * The item was found in the list. It shouldn't have been there
549 * If we get here, we have found a spot for our item. The parentNodeLink
550 * pointer points to the pointer that we have to modify.
551 * The curNode should be NULL. We just have to establish the link and Voila!
553 assert(curNode
==NULL
);
554 assert(parentNodeLink
!=NULL
);
555 assert(*parentNodeLink
==NULL
);
557 *parentNodeLink
=nodeToAdd
;
561 * OLEDD_ExtractDropTarget()
563 * Removes the target node from the tree.
565 static DropTargetNode
* OLEDD_ExtractDropTarget(HWND32 hwndOfTarget
)
567 DropTargetNode
* curNode
;
568 DropTargetNode
** parentNodeLink
;
571 * Iterate the tree to find the insertion point.
573 curNode
= targetListHead
;
574 parentNodeLink
= &targetListHead
;
576 while (curNode
!=NULL
)
578 if (hwndOfTarget
<curNode
->hwndTarget
)
581 * If the node we want to add has a smaller HWND, go left
583 parentNodeLink
= &curNode
->prevDropTarget
;
584 curNode
= curNode
->prevDropTarget
;
586 else if (hwndOfTarget
>curNode
->hwndTarget
)
589 * If the node we want to add has a larger HWND, go right
591 parentNodeLink
= &curNode
->nextDropTarget
;
592 curNode
= curNode
->nextDropTarget
;
597 * The item was found in the list. Detach it from it's parent and
598 * re-insert it's kids in the tree.
600 assert(parentNodeLink
!=NULL
);
601 assert(*parentNodeLink
==curNode
);
604 * We arbitrately re-attach the left sub-tree to the parent.
606 *parentNodeLink
= curNode
->prevDropTarget
;
609 * And we re-insert the right subtree
611 if (curNode
->nextDropTarget
!=NULL
)
613 OLEDD_InsertDropTarget(curNode
->nextDropTarget
);
617 * The node we found is still a valid node once we complete
618 * the unlinking of the kids.
620 curNode
->nextDropTarget
=NULL
;
621 curNode
->prevDropTarget
=NULL
;
628 * If we get here, the node is not in the tree
634 * OLEDD_FindDropTarget()
636 * Finds information about the drop target.
638 static DropTargetNode
* OLEDD_FindDropTarget(HWND32 hwndOfTarget
)
640 DropTargetNode
* curNode
;
643 * Iterate the tree to find the HWND value.
645 curNode
= targetListHead
;
647 while (curNode
!=NULL
)
649 if (hwndOfTarget
<curNode
->hwndTarget
)
652 * If the node we want to add has a smaller HWND, go left
654 curNode
= curNode
->prevDropTarget
;
656 else if (hwndOfTarget
>curNode
->hwndTarget
)
659 * If the node we want to add has a larger HWND, go right
661 curNode
= curNode
->nextDropTarget
;
666 * The item was found in the list.
673 * If we get here, the item is not in the list
679 * OLEDD_DragTrackerWindowProc()
681 * This method is the WindowProcedure of the drag n drop tracking
682 * window. During a drag n Drop operation, an invisible window is created
683 * to receive the user input and act upon it. This procedure is in charge
686 static LRESULT WINAPI
OLEDD_DragTrackerWindowProc(
696 LPCREATESTRUCT32A createStruct
= (LPCREATESTRUCT32A
)lParam
;
698 SetWindowLong32A(hwnd
, 0, (LONG
)createStruct
->lpCreateParams
);
705 TrackerWindowInfo
* trackerInfo
= (TrackerWindowInfo
*)GetWindowLong32A(hwnd
, 0);
709 * Get the current mouse position in screen coordinates.
711 mousePos
.x
= LOWORD(lParam
);
712 mousePos
.y
= HIWORD(lParam
);
713 ClientToScreen32(hwnd
, &mousePos
);
716 * Track the movement of the mouse.
718 OLEDD_TrackMouseMove(trackerInfo
, mousePos
, wParam
);
729 TrackerWindowInfo
* trackerInfo
= (TrackerWindowInfo
*)GetWindowLong32A(hwnd
, 0);
733 * Get the current mouse position in screen coordinates.
735 mousePos
.x
= LOWORD(lParam
);
736 mousePos
.y
= HIWORD(lParam
);
737 ClientToScreen32(hwnd
, &mousePos
);
740 * Notify everyone that the button state changed
741 * TODO: Check if the "escape" key was pressed.
743 OLEDD_TrackStateChange(trackerInfo
, mousePos
, wParam
);
750 * This is a window proc after all. Let's call the default.
752 return DefWindowProc32A (hwnd
, uMsg
, wParam
, lParam
);
756 * OLEDD_TrackMouseMove()
758 * This method is invoked while a drag and drop operation is in effect.
759 * it will generate the appropriate callbacks in the drop source
760 * and drop target. It will also provide the expected feedback to
764 * trackerInfo - Pointer to the structure identifying the
765 * drag & drop operation that is currently
767 * mousePos - Current position of the mouse in screen
769 * keyState - Contains the state of the shift keys and the
770 * mouse buttons (MK_LBUTTON and the like)
772 static void OLEDD_TrackMouseMove(
773 TrackerWindowInfo
* trackerInfo
,
777 HWND32 hwndNewTarget
= 0;
781 * Get the handle of the window under the mouse
783 hwndNewTarget
= WindowFromPoint32(mousePos
);
786 * If we are hovering over the same target as before, send the
787 * DragOver notification
789 if ( (trackerInfo
->curDragTarget
!= 0) &&
790 (trackerInfo
->curDragTargetHWND
==hwndNewTarget
) )
792 POINTL mousePosParam
;
795 * The documentation tells me that the coordinate should be in the target
796 * window's coordinate space. However, the tests I made tell me the
797 * coordinates should be in screen coordinates.
799 mousePosParam
.x
= mousePos
.x
;
800 mousePosParam
.y
= mousePos
.y
;
802 IDropTarget_DragOver(trackerInfo
->curDragTarget
,
805 trackerInfo
->pdwEffect
);
809 DropTargetNode
* newDropTargetNode
= 0;
812 * If we changed window, we have to notify our old target and check for
815 if (trackerInfo
->curDragTarget
!=0)
817 IDropTarget_DragLeave(trackerInfo
->curDragTarget
);
821 * Make sure we're hovering over a window.
823 if (hwndNewTarget
!=0)
826 * Find-out if there is a drag target under the mouse
828 newDropTargetNode
= OLEDD_FindDropTarget(hwndNewTarget
);
830 trackerInfo
->curDragTargetHWND
= hwndNewTarget
;
831 trackerInfo
->curDragTarget
= newDropTargetNode
? newDropTargetNode
->dropTarget
: 0;
834 * If there is, notify it that we just dragged-in
836 if (trackerInfo
->curDragTarget
!=0)
838 POINTL mousePosParam
;
841 * The documentation tells me that the coordinate should be in the target
842 * window's coordinate space. However, the tests I made tell me the
843 * coordinates should be in screen coordinates.
845 mousePosParam
.x
= mousePos
.x
;
846 mousePosParam
.y
= mousePos
.y
;
848 IDropTarget_DragEnter(trackerInfo
->curDragTarget
,
849 trackerInfo
->dataObject
,
852 trackerInfo
->pdwEffect
);
858 * The mouse is not over a window so we don't track anything.
860 trackerInfo
->curDragTargetHWND
= 0;
861 trackerInfo
->curDragTarget
= 0;
866 * Now that we have done that, we have to tell the source to give
867 * us feedback on the work being done by the target. If we don't
868 * have a target, simulate no effect.
870 if (trackerInfo
->curDragTarget
==0)
872 *trackerInfo
->pdwEffect
= DROPEFFECT_NONE
;
875 hr
= IDropSource_GiveFeedback(trackerInfo
->dropSource
,
876 *trackerInfo
->pdwEffect
);
879 * When we ask for feedback from the drop source, sometimes it will
880 * do all the necessary work and sometimes it will not handle it
881 * when that's the case, we must display the standard drag and drop
884 if (hr
==DRAGDROP_S_USEDEFAULTCURSORS
)
886 if ( (*trackerInfo
->pdwEffect
& DROPEFFECT_MOVE
) ||
887 (*trackerInfo
->pdwEffect
& DROPEFFECT_COPY
) ||
888 (*trackerInfo
->pdwEffect
& DROPEFFECT_LINK
) )
890 SetCursor32(LoadCursor32A(0, IDC_SIZEALL32A
));
894 SetCursor32(LoadCursor32A(0, IDC_NO32A
));
900 * OLEDD_TrackStateChange()
902 * This method is invoked while a drag and drop operation is in effect.
903 * It is used to notify the drop target/drop source callbacks when
904 * the state of the keyboard or mouse button change.
907 * trackerInfo - Pointer to the structure identifying the
908 * drag & drop operation that is currently
910 * mousePos - Current position of the mouse in screen
912 * keyState - Contains the state of the shift keys and the
913 * mouse buttons (MK_LBUTTON and the like)
915 static void OLEDD_TrackStateChange(
916 TrackerWindowInfo
* trackerInfo
,
921 * Ask the drop source what to do with the operation.
923 trackerInfo
->returnValue
= IDropSource_QueryContinueDrag(
924 trackerInfo
->dropSource
,
925 trackerInfo
->escPressed
,
929 * All the return valued will stop the operation except the S_OK
932 if (trackerInfo
->returnValue
!=S_OK
)
935 * Make sure the message loop in DoDragDrop stops
937 trackerInfo
->trackingDone
= TRUE
;
940 * Release the mouse in case the drop target decides to show a popup
941 * or a menu or something.
946 * If we end-up over a target, drop the object in the target or
947 * inform the target that the operation was cancelled.
949 if (trackerInfo
->curDragTarget
!=0)
951 switch (trackerInfo
->returnValue
)
954 * If the source wants us to complete the operation, we tell
955 * the drop target that we just dropped the object in it.
957 case DRAGDROP_S_DROP
:
959 POINTL mousePosParam
;
962 * The documentation tells me that the coordinate should be
963 * in the target window's coordinate space. However, the tests
964 * I made tell me the coordinates should be in screen coordinates.
966 mousePosParam
.x
= mousePos
.x
;
967 mousePosParam
.y
= mousePos
.y
;
969 IDropTarget_Drop(trackerInfo
->curDragTarget
,
970 trackerInfo
->dataObject
,
973 trackerInfo
->pdwEffect
);
977 * If the source told us that we should cancel, fool the drop
978 * target by telling it that the mouse left it's window.
980 case DRAGDROP_S_CANCEL
:
981 IDropTarget_DragLeave(trackerInfo
->curDragTarget
);
989 * OLEDD_GetButtonState()
991 * This method will use the current state of the keyboard to build
992 * a button state mask equivalent to the one passed in the
993 * WM_MOUSEMOVE wParam.
995 static DWORD
OLEDD_GetButtonState()
997 BYTE keyboardState
[256];
1000 GetKeyboardState(keyboardState
);
1002 if ( (keyboardState
[VK_SHIFT
] & 0x80) !=0)
1003 keyMask
|= MK_SHIFT
;
1005 if ( (keyboardState
[VK_CONTROL
] & 0x80) !=0)
1006 keyMask
|= MK_CONTROL
;
1008 if ( (keyboardState
[VK_LBUTTON
] & 0x80) !=0)
1009 keyMask
|= MK_LBUTTON
;
1011 if ( (keyboardState
[VK_RBUTTON
] & 0x80) !=0)
1012 keyMask
|= MK_RBUTTON
;
1014 if ( (keyboardState
[VK_MBUTTON
] & 0x80) !=0)
1015 keyMask
|= MK_MBUTTON
;