Added some digital video interface
[wine/multimedia.git] / ole / ole2.c
blobc19b294e61ef735d7b2c89da0f49974b5a86e385
1 /*
2 * OLE2 library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1999 Francis Beaudet
6 * Copyright 1999 Noel Borthwick
7 */
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <strings.h>
13 #include "winuser.h"
14 #include "winerror.h"
15 #include "ole2.h"
16 #include "process.h"
17 #include "hook.h"
18 #include "commctrl.h"
19 #include "wine/obj_clientserver.h"
20 #include "debug.h"
21 #include "ole2ver.h"
22 #include "winreg.h"
24 DEFAULT_DEBUG_CHANNEL(ole)
26 /******************************************************************************
27 * These are static/global variables and internal data structures that the
28 * OLE module uses to maintain it's state.
30 typedef struct tagDropTargetNode
32 HWND hwndTarget;
33 IDropTarget* dropTarget;
34 struct tagDropTargetNode* prevDropTarget;
35 struct tagDropTargetNode* nextDropTarget;
36 } DropTargetNode;
38 typedef struct tagTrackerWindowInfo
40 IDataObject* dataObject;
41 IDropSource* dropSource;
42 DWORD dwOKEffect;
43 DWORD* pdwEffect;
44 BOOL trackingDone;
45 HRESULT returnValue;
47 BOOL escPressed;
48 HWND curDragTargetHWND;
49 IDropTarget* curDragTarget;
50 } TrackerWindowInfo;
52 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
54 HWND hwndFrame; /* The containers frame window */
55 HWND hwndActiveObject; /* The active objects window */
56 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
57 HMENU hmenuCombined; /* The combined menu */
58 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
59 } OleMenuDescriptor;
61 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
63 DWORD tid; /* Thread Id */
64 HANDLE hHeap; /* Heap this is allocated from */
65 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
66 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
67 } OleMenuHookItem;
70 * Dynamic pointer array of per thread message hooks (maintained by OleSetMenuDescriptor)
72 static HDPA OLEMenu_MsgHookDPA = NULL;
75 * This is the lock count on the OLE library. It is controlled by the
76 * OLEInitialize/OLEUninitialize methods.
78 static ULONG OLE_moduleLockCount = 0;
81 * Name of our registered window class.
83 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
86 * This is the head of the Drop target container.
88 static DropTargetNode* targetListHead = NULL;
90 /******************************************************************************
91 * These are the prototypes of miscelaneous utility methods
93 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
95 /******************************************************************************
96 * These are the prototypes of the utility methods used to manage a shared menu
98 static void OLEMenu_Initialize();
99 static void OLEMenu_UnInitialize();
100 BOOL OLEMenu_InstallHooks( DWORD tid );
101 BOOL OLEMenu_UnInstallHooks( DWORD tid );
102 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid, INT *pixHook );
103 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
104 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
105 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
106 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
108 /******************************************************************************
109 * These are the prototypes of the utility methods used for OLE Drag n Drop
111 static void OLEDD_Initialize();
112 static void OLEDD_UnInitialize();
113 static void OLEDD_InsertDropTarget(
114 DropTargetNode* nodeToAdd);
115 static DropTargetNode* OLEDD_ExtractDropTarget(
116 HWND hwndOfTarget);
117 static DropTargetNode* OLEDD_FindDropTarget(
118 HWND hwndOfTarget);
119 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
120 HWND hwnd,
121 UINT uMsg,
122 WPARAM wParam,
123 LPARAM lParam);
124 static void OLEDD_TrackMouseMove(
125 TrackerWindowInfo* trackerInfo,
126 POINT mousePos,
127 DWORD keyState);
128 static void OLEDD_TrackStateChange(
129 TrackerWindowInfo* trackerInfo,
130 POINT mousePos,
131 DWORD keyState);
132 static DWORD OLEDD_GetButtonState();
135 /******************************************************************************
136 * OleBuildVersion [OLE2.1]
138 DWORD WINAPI OleBuildVersion(void)
140 TRACE(ole,"(void)\n");
141 return (rmm<<16)+rup;
144 /***********************************************************************
145 * OleInitialize (OLE2.2) (OLE32.108)
147 HRESULT WINAPI OleInitialize(LPVOID reserved)
149 HRESULT hr;
151 TRACE(ole, "(%p)\n", reserved);
154 * The first duty of the OleInitialize is to initialize the COM libraries.
156 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
159 * If the CoInitializeEx call failed, the OLE libraries can't be
160 * initialized.
162 if (FAILED(hr))
163 return hr;
166 * Then, it has to initialize the OLE specific modules.
167 * This includes:
168 * Clipboard
169 * Drag and Drop
170 * Object linking and Embedding
171 * In-place activation
173 if (OLE_moduleLockCount==0)
176 * Initialize the libraries.
178 TRACE(ole, "() - Initializing the OLE libraries\n");
181 * Drag and Drop
183 OLEDD_Initialize();
186 * OLE shared menu
188 OLEMenu_Initialize();
192 * Then, we increase the lock count on the OLE module.
194 OLE_moduleLockCount++;
196 return hr;
199 /******************************************************************************
200 * CoGetCurrentProcess [COMPOBJ.34] [OLE2.2][OLE32.108]
202 * NOTES
203 * Is DWORD really the correct return type for this function?
205 DWORD WINAPI CoGetCurrentProcess(void) {
206 return (DWORD)PROCESS_Current();
209 /******************************************************************************
210 * OleUninitialize [OLE2.3] [OLE32.131]
212 void WINAPI OleUninitialize(void)
214 TRACE(ole, "()\n");
217 * Decrease the lock count on the OLE module.
219 OLE_moduleLockCount--;
222 * If we hit the bottom of the lock stack, free the libraries.
224 if (OLE_moduleLockCount==0)
227 * Actually free the libraries.
229 TRACE(ole, "() - Freeing the last reference count\n");
232 * Drag and Drop
234 OLEDD_UnInitialize();
237 * OLE shared menu
239 OLEMenu_UnInitialize();
243 * Then, uninitialize the COM libraries.
245 CoUninitialize();
248 /***********************************************************************
249 * OleFlushClipboard [OLE2.76]
251 HRESULT WINAPI OleFlushClipboard16(void)
253 return S_OK;
256 /***********************************************************************
257 * OleSetClipboard [OLE32.127]
259 HRESULT WINAPI OleSetClipboard(LPVOID pDataObj)
261 FIXME(ole,"(%p), stub!\n", pDataObj);
262 return S_OK;
265 /******************************************************************************
266 * CoRegisterMessageFilter32 [OLE32.38]
268 HRESULT WINAPI CoRegisterMessageFilter(
269 LPMESSAGEFILTER lpMessageFilter, /* Pointer to interface */
270 LPMESSAGEFILTER *lplpMessageFilter /* Indirect pointer to prior instance if non-NULL */
272 FIXME(ole,"stub\n");
273 if (lplpMessageFilter) {
274 *lplpMessageFilter = NULL;
276 return S_OK;
279 /******************************************************************************
280 * OleInitializeWOW [OLE32.109]
282 HRESULT WINAPI OleInitializeWOW(DWORD x) {
283 FIXME(ole,"(0x%08lx),stub!\n",x);
284 return 0;
287 /***********************************************************************
288 * RegisterDragDrop16 (OLE2.35)
290 HRESULT WINAPI RegisterDragDrop16(
291 HWND16 hwnd,
292 LPDROPTARGET pDropTarget
294 FIXME(ole,"(0x%04x,%p),stub!\n",hwnd,pDropTarget);
295 return S_OK;
298 /***********************************************************************
299 * RegisterDragDrop32 (OLE32.139)
301 HRESULT WINAPI RegisterDragDrop(
302 HWND hwnd,
303 LPDROPTARGET pDropTarget)
305 DropTargetNode* dropTargetInfo;
307 TRACE(ole,"(0x%x,%p)\n", hwnd, pDropTarget);
310 * First, check if the window is already registered.
312 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
314 if (dropTargetInfo!=NULL)
315 return DRAGDROP_E_ALREADYREGISTERED;
318 * If it's not there, we can add it. We first create a node for it.
320 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
322 if (dropTargetInfo==NULL)
323 return E_OUTOFMEMORY;
325 dropTargetInfo->hwndTarget = hwnd;
326 dropTargetInfo->prevDropTarget = NULL;
327 dropTargetInfo->nextDropTarget = NULL;
330 * Don't forget that this is an interface pointer, need to nail it down since
331 * we keep a copy of it.
333 dropTargetInfo->dropTarget = pDropTarget;
334 IDropTarget_AddRef(dropTargetInfo->dropTarget);
336 OLEDD_InsertDropTarget(dropTargetInfo);
338 return S_OK;
341 /***********************************************************************
342 * RevokeDragDrop16 (OLE2.36)
344 HRESULT WINAPI RevokeDragDrop16(
345 HWND16 hwnd
347 FIXME(ole,"(0x%04x),stub!\n",hwnd);
348 return S_OK;
351 /***********************************************************************
352 * RevokeDragDrop32 (OLE32.141)
354 HRESULT WINAPI RevokeDragDrop(
355 HWND hwnd)
357 DropTargetNode* dropTargetInfo;
359 TRACE(ole,"(0x%x)\n", hwnd);
362 * First, check if the window is already registered.
364 dropTargetInfo = OLEDD_ExtractDropTarget(hwnd);
367 * If it ain't in there, it's an error.
369 if (dropTargetInfo==NULL)
370 return DRAGDROP_E_NOTREGISTERED;
373 * If it's in there, clean-up it's used memory and
374 * references
376 IDropTarget_Release(dropTargetInfo->dropTarget);
377 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
379 return S_OK;
382 /***********************************************************************
383 * OleRegGetUserType (OLE32.122)
385 * This implementation of OleRegGetUserType ignores the dwFormOfType
386 * parameter and always returns the full name of the object. This is
387 * not too bad since this is the case for many objects because of the
388 * way they are registered.
390 HRESULT WINAPI OleRegGetUserType(
391 REFCLSID clsid,
392 DWORD dwFormOfType,
393 LPOLESTR* pszUserType)
395 char xclsid[50];
396 char keyName[60];
397 DWORD dwKeyType;
398 DWORD cbData;
399 HKEY clsidKey;
400 LONG hres;
403 * Initialize the out parameter.
405 *pszUserType = NULL;
408 * Build the key name we're looking for
410 WINE_StringFromCLSID((LPCLSID)clsid, xclsid);
412 strcpy(keyName, "CLSID\\");
413 strcat(keyName, xclsid);
414 strcat(keyName, "\\");
416 TRACE(ole,"(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
419 * Open the class id Key
421 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
422 keyName,
423 &clsidKey);
425 if (hres != ERROR_SUCCESS)
426 return REGDB_E_CLASSNOTREG;
429 * Retrieve the size of the name string.
431 cbData = 0;
433 hres = RegQueryValueExA(clsidKey,
435 NULL,
436 &dwKeyType,
437 NULL,
438 &cbData);
440 if (hres!=ERROR_SUCCESS)
442 RegCloseKey(clsidKey);
443 return REGDB_E_READREGDB;
447 * Allocate a buffer for the registry value.
449 *pszUserType = CoTaskMemAlloc(cbData);
451 if (*pszUserType==NULL)
453 RegCloseKey(clsidKey);
454 return E_OUTOFMEMORY;
457 hres = RegQueryValueExA(HKEY_CLASSES_ROOT,
459 NULL,
460 &dwKeyType,
461 (LPBYTE)*pszUserType,
462 &cbData);
464 RegCloseKey(clsidKey);
466 if (hres!=ERROR_SUCCESS)
468 CoTaskMemFree(*pszUserType);
469 *pszUserType=NULL;
471 return REGDB_E_READREGDB;
474 return S_OK;
477 /***********************************************************************
478 * DoDragDrop32 [OLE32.65]
480 HRESULT WINAPI DoDragDrop (
481 IDataObject *pDataObject, /* ptr to the data obj */
482 IDropSource* pDropSource, /* ptr to the source obj */
483 DWORD dwOKEffect, /* effects allowed by the source */
484 DWORD *pdwEffect) /* ptr to effects of the source */
486 TrackerWindowInfo trackerInfo;
487 HWND hwndTrackWindow;
488 MSG msg;
490 TRACE(ole,"(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
493 * Setup the drag n drop tracking window.
495 trackerInfo.dataObject = pDataObject;
496 trackerInfo.dropSource = pDropSource;
497 trackerInfo.dwOKEffect = dwOKEffect;
498 trackerInfo.pdwEffect = pdwEffect;
499 trackerInfo.trackingDone = FALSE;
500 trackerInfo.escPressed = FALSE;
501 trackerInfo.curDragTargetHWND = 0;
502 trackerInfo.curDragTarget = 0;
504 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
505 "TrackerWindow",
506 WS_POPUP,
507 CW_USEDEFAULT, CW_USEDEFAULT,
508 CW_USEDEFAULT, CW_USEDEFAULT,
512 (LPVOID)&trackerInfo);
514 if (hwndTrackWindow!=0)
517 * Capture the mouse input
519 SetCapture(hwndTrackWindow);
522 * Pump messages. All mouse input should go the the capture window.
524 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
526 if ( (msg.message >= WM_KEYFIRST) &&
527 (msg.message <= WM_KEYFIRST) )
530 * When keyboard messages are sent to windows on this thread, we
531 * want to ignore notify the drop source that the state changed.
532 * in the case of the Escape key, we also notify the drop source
533 * we give it a special meaning.
535 if ( (msg.message==WM_KEYDOWN) &&
536 (msg.wParam==VK_ESCAPE) )
538 trackerInfo.escPressed = TRUE;
542 * Notify the drop source.
544 OLEDD_TrackStateChange(&trackerInfo,
545 msg.pt,
546 OLEDD_GetButtonState());
548 else
551 * Dispatch the messages only when it's not a keyboard message.
553 DispatchMessageA(&msg);
558 * Destroy the temporary window.
560 DestroyWindow(hwndTrackWindow);
562 return trackerInfo.returnValue;
565 return E_FAIL;
568 /***********************************************************************
569 * OleQueryLinkFromData32 [OLE32.118]
571 HRESULT WINAPI OleQueryLinkFromData(
572 IDataObject* pSrcDataObject)
574 FIXME(ole,"(%p),stub!\n", pSrcDataObject);
575 return S_OK;
578 /***********************************************************************
579 * OleRegGetMiscStatus [OLE32.121]
581 HRESULT WINAPI OleRegGetMiscStatus(
582 REFCLSID clsid,
583 DWORD dwAspect,
584 DWORD* pdwStatus)
586 char xclsid[50];
587 char keyName[60];
588 HKEY clsidKey;
589 HKEY miscStatusKey;
590 HKEY aspectKey;
591 LONG result;
594 * Initialize the out parameter.
596 *pdwStatus = 0;
599 * Build the key name we're looking for
601 WINE_StringFromCLSID((LPCLSID)clsid, xclsid);
603 strcpy(keyName, "CLSID\\");
604 strcat(keyName, xclsid);
605 strcat(keyName, "\\");
607 TRACE(ole,"(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
610 * Open the class id Key
612 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
613 keyName,
614 &clsidKey);
616 if (result != ERROR_SUCCESS)
617 return REGDB_E_CLASSNOTREG;
620 * Get the MiscStatus
622 result = RegOpenKeyA(clsidKey,
623 "MiscStatus",
624 &miscStatusKey);
627 if (result != ERROR_SUCCESS)
629 RegCloseKey(clsidKey);
630 return REGDB_E_READREGDB;
634 * Read the default value
636 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
639 * Open the key specific to the requested aspect.
641 sprintf(keyName, "%ld", dwAspect);
643 result = RegOpenKeyA(miscStatusKey,
644 keyName,
645 &aspectKey);
647 if (result == ERROR_SUCCESS)
649 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
650 RegCloseKey(aspectKey);
654 * Cleanup
656 RegCloseKey(miscStatusKey);
657 RegCloseKey(clsidKey);
659 return S_OK;
662 /******************************************************************************
663 * OleSetContainedObject [OLE32.128]
665 HRESULT WINAPI OleSetContainedObject(
666 LPUNKNOWN pUnknown,
667 BOOL fContained)
669 IRunnableObject* runnable = NULL;
670 HRESULT hres;
672 TRACE(ole,"(%p,%x), stub!\n", pUnknown, fContained);
674 hres = IUnknown_QueryInterface(pUnknown,
675 &IID_IRunnableObject,
676 (void**)&runnable);
678 if (SUCCEEDED(hres))
680 hres = IRunnableObject_SetContainedObject(runnable, fContained);
682 IRunnableObject_Release(runnable);
684 return hres;
687 return S_OK;
690 /******************************************************************************
691 * OleLoad [OLE32.112]
693 HRESULT WINAPI OleLoad(
694 LPSTORAGE pStg,
695 REFIID riid,
696 LPOLECLIENTSITE pClientSite,
697 LPVOID* ppvObj)
699 IPersistStorage* persistStorage = NULL;
700 IOleObject* oleObject = NULL;
701 STATSTG storageInfo;
702 HRESULT hres;
704 TRACE(ole,"(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
707 * TODO, Conversion ... OleDoAutoConvert
711 * Get the class ID for the object.
713 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
716 * Now, try and create the handler for the object
718 hres = CoCreateInstance(&storageInfo.clsid,
719 NULL,
720 CLSCTX_INPROC_HANDLER,
721 &IID_IOleObject,
722 (void**)&oleObject);
725 * If that fails, as it will most times, load the default
726 * OLE handler.
728 if (FAILED(hres))
730 hres = OleCreateDefaultHandler(&storageInfo.clsid,
731 NULL,
732 &IID_IOleObject,
733 (void**)&oleObject);
737 * If we couldn't find a handler... this is bad. Abort the whole thing.
739 if (FAILED(hres))
740 return hres;
743 * Inform the new object of it's client site.
745 hres = IOleObject_SetClientSite(oleObject, pClientSite);
748 * Initialize the object with it's IPersistStorage interface.
750 hres = IOleObject_QueryInterface(oleObject,
751 &IID_IPersistStorage,
752 (void**)&persistStorage);
754 if (SUCCEEDED(hres))
756 IPersistStorage_Load(persistStorage, pStg);
758 IPersistStorage_Release(persistStorage);
759 persistStorage = NULL;
763 * Return the requested interface to the caller.
765 hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
768 * Cleanup interfaces used internally
770 IOleObject_Release(oleObject);
772 return hres;
775 /***********************************************************************
776 * OleSave [OLE32.124]
778 HRESULT WINAPI OleSave(
779 LPPERSISTSTORAGE pPS,
780 LPSTORAGE pStg,
781 BOOL fSameAsLoad)
783 HRESULT hres;
784 CLSID objectClass;
786 TRACE(ole,"(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
789 * First, we transfer the class ID (if available)
791 hres = IPersistStorage_GetClassID(pPS, &objectClass);
793 if (SUCCEEDED(hres))
795 WriteClassStg(pStg, &objectClass);
799 * Then, we ask the object to save itself to the
800 * storage. If it is successful, we commit the storage.
802 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
804 if (SUCCEEDED(hres))
806 IStorage_Commit(pStg,
807 STGC_DEFAULT);
810 return hres;
814 /***********************************************************************
815 * OleGetClipboard32 [OLE32.105]
817 HRESULT WINAPI OleGetClipboard(
818 IDataObject** ppDataObj)
820 FIXME(ole,"(%p),stub!\n", ppDataObj);
822 if (ppDataObj)
823 *ppDataObj=0;
825 return E_FAIL;
829 /**************************************************************************
830 * Internal methods to manage the shared OLE menu in response to the
831 * OLE***MenuDescriptor API
834 /***
835 * OLEMenu_Initialize()
837 * Initializes the OLEMENU data structures.
839 static void OLEMenu_Initialize()
841 /* Create a dynamic pointer array to store the hook handles */
842 if ( !OLEMenu_MsgHookDPA )
843 OLEMenu_MsgHookDPA = DPA_CreateEx( 2, GetProcessHeap() );
846 /***
847 * OLEMenu_UnInitialize()
849 * Releases the OLEMENU data structures.
851 static void OLEMenu_UnInitialize()
853 /* Release the hook table */
854 if ( OLEMenu_MsgHookDPA )
855 DPA_Destroy( OLEMenu_MsgHookDPA );
857 OLEMenu_MsgHookDPA = NULL;
860 /*************************************************************************
861 * OLEMenu_InstallHooks
862 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
864 * RETURNS: TRUE if message hooks were succesfully installed
865 * FALSE on failure
867 BOOL OLEMenu_InstallHooks( DWORD tid )
869 OleMenuHookItem *pHookItem = NULL;
871 if ( !OLEMenu_MsgHookDPA ) /* No hook table? Create one */
873 /* Create a dynamic pointer array to store the hook handles */
874 if ( !(OLEMenu_MsgHookDPA = DPA_CreateEx( 2, GetProcessHeap() )) )
875 return FALSE;
878 /* Create an entry for the hook table */
879 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
880 sizeof(OleMenuHookItem)) ) )
881 return FALSE;
883 pHookItem->tid = tid;
884 pHookItem->hHeap = GetProcessHeap();
886 /* Install a thread scope message hook for WH_GETMESSAGE */
887 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
888 0, GetCurrentThreadId() );
889 if ( !pHookItem->GetMsg_hHook )
890 goto CLEANUP;
892 /* Install a thread scope message hook for WH_CALLWNDPROC */
893 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
894 0, GetCurrentThreadId() );
895 if ( !pHookItem->CallWndProc_hHook )
896 goto CLEANUP;
898 /* Insert the hook table entry */
899 if ( -1 == DPA_InsertPtr( OLEMenu_MsgHookDPA, 0, pHookItem ) )
900 goto CLEANUP;
902 return TRUE;
904 CLEANUP:
905 /* Unhook any hooks */
906 if ( pHookItem->GetMsg_hHook )
907 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
908 if ( pHookItem->CallWndProc_hHook )
909 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
910 /* Release the hook table entry */
911 HeapFree(pHookItem->hHeap, 0, pHookItem );
913 return FALSE;
916 /*************************************************************************
917 * OLEMenu_UnInstallHooks
918 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
920 * RETURNS: TRUE if message hooks were succesfully installed
921 * FALSE on failure
923 BOOL OLEMenu_UnInstallHooks( DWORD tid )
925 INT ixHook;
926 OleMenuHookItem *pHookItem = NULL;
928 if ( !OLEMenu_MsgHookDPA ) /* No hooks set */
929 return TRUE;
931 /* Lookup the hHook index for this tid */
932 if ( !OLEMenu_IsHookInstalled( tid , &ixHook ) )
933 return TRUE;
935 /* Remove the hook entry from the table(the pointer itself is not deleted) */
936 if ( !( pHookItem = DPA_DeletePtr(OLEMenu_MsgHookDPA, ixHook) ) )
937 return FALSE;
939 /* Uninstall the hooks installed for this thread */
940 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
941 goto CLEANUP;
942 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
943 goto CLEANUP;
945 /* Release the hook table entry */
946 HeapFree(pHookItem->hHeap, 0, pHookItem );
948 return TRUE;
950 CLEANUP:
951 /* Release the hook table entry */
952 if (pHookItem)
953 HeapFree(pHookItem->hHeap, 0, pHookItem );
955 return FALSE;
958 /*************************************************************************
959 * OLEMenu_IsHookInstalled
960 * Tests if OLEMenu hooks have been installed for a thread
962 * RETURNS: The pointer and index of the hook table entry for the tid
963 * NULL and -1 for the index if no hooks were installed for this thread
965 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid, INT *pixHook )
967 INT ixHook;
968 OleMenuHookItem *pHookItem = NULL;
970 if ( pixHook )
971 *pixHook = -1;
973 if ( !OLEMenu_MsgHookDPA ) /* No hooks set */
974 return NULL;
976 /* Do a simple linear search for an entry whose tid matches ours.
977 * We really need a map but efficiency is not a concern here. */
978 for( ixHook = 0; ; ixHook++ )
980 /* Retrieve the hook entry */
981 if ( !( pHookItem = DPA_GetPtr(OLEMenu_MsgHookDPA, ixHook) ) )
982 return NULL;
984 if ( tid == pHookItem->tid )
986 if ( pixHook )
987 *pixHook = ixHook;
988 return pHookItem;
992 return NULL;
995 /***********************************************************************
996 * OLEMenu_FindMainMenuIndex
998 * Used by OLEMenu API to find the top level group a menu item belongs to.
999 * On success pnPos contains the index of the item in the top level menu group
1001 * RETURNS: TRUE if the ID was found, FALSE on failure
1003 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1005 UINT i, nItems;
1007 nItems = GetMenuItemCount( hMainMenu );
1009 for (i = 0; i < nItems; i++)
1011 HMENU hsubmenu;
1013 /* Is the current item a submenu? */
1014 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1016 /* If the handle is the same we're done */
1017 if ( hsubmenu == hPopupMenu )
1019 if (pnPos)
1020 *pnPos = i;
1021 return TRUE;
1023 /* Recursively search without updating pnPos */
1024 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1026 if (pnPos)
1027 *pnPos = i;
1028 return TRUE;
1033 return FALSE;
1036 /***********************************************************************
1037 * OLEMenu_SetIsServerMenu
1039 * Checks whether a popup menu belongs to a shared menu group which is
1040 * owned by the server, and sets the menu descriptor state accordingly.
1041 * All menu messages from these groups should be routed to the server.
1043 * RETURNS: TRUE if the popup menu is part of a server owned group
1044 * FASE if the popup menu is part of a container owned group
1046 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1048 UINT nPos = 0, nWidth, i;
1050 pOleMenuDescriptor->bIsServerItem = FALSE;
1052 /* Don't bother searching if the popup is the combined menu itself */
1053 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1054 return FALSE;
1056 /* Find the menu item index in the shared OLE menu that this item belongs to */
1057 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1058 return FALSE;
1060 /* The group widths array has counts for the number of elements
1061 * in the groups File, Edit, Container, Object, Window, Help.
1062 * The Edit, Object & Help groups belong to the server object
1063 * and the other three belong to the container.
1064 * Loop thru the group widths and locate the group we are a member of.
1066 for ( i = 0, nWidth = 0; i < 6; i++ )
1068 nWidth += pOleMenuDescriptor->mgw.width[i];
1069 if ( nPos < nWidth )
1071 /* Odd elements are server menu widths */
1072 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1073 break;
1077 return pOleMenuDescriptor->bIsServerItem;
1080 /*************************************************************************
1081 * OLEMenu_CallWndProc
1082 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1083 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1085 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1087 LPCWPSTRUCT pMsg = NULL;
1088 HOLEMENU hOleMenu = 0;
1089 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1090 OleMenuHookItem *pHookItem = NULL;
1091 WORD fuFlags;
1093 TRACE(ole,"%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1095 /* Check if we're being asked to process the message */
1096 if ( HC_ACTION != code )
1097 goto NEXTHOOK;
1099 /* Retrieve the current message being dispatched from lParam */
1100 pMsg = (LPCWPSTRUCT)lParam;
1102 /* Check if the message is destined for a window we are interested in:
1103 * If the window has an OLEMenu property we may need to dispatch
1104 * the menu message to its active objects window instead. */
1106 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1107 if ( !hOleMenu )
1108 goto NEXTHOOK;
1110 /* Get the menu descriptor */
1111 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1112 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1113 goto NEXTHOOK;
1115 /* Process menu messages */
1116 switch( pMsg->message )
1118 case WM_INITMENU:
1120 /* Reset the menu descriptor state */
1121 pOleMenuDescriptor->bIsServerItem = FALSE;
1123 /* Send this message to the server as well */
1124 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1125 pMsg->message, pMsg->wParam, pMsg->lParam );
1126 goto NEXTHOOK;
1129 case WM_INITMENUPOPUP:
1131 /* Save the state for whether this is a server owned menu */
1132 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1133 break;
1136 case WM_MENUSELECT:
1138 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1139 if ( fuFlags & MF_SYSMENU )
1140 goto NEXTHOOK;
1142 /* Save the state for whether this is a server owned popup menu */
1143 else if ( fuFlags & MF_POPUP )
1144 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1146 break;
1149 case WM_DRAWITEM:
1151 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1152 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1153 goto NEXTHOOK; /* Not a menu message */
1155 break;
1158 default:
1159 goto NEXTHOOK;
1162 /* If the message was for the server dispatch it accordingly */
1163 if ( pOleMenuDescriptor->bIsServerItem )
1165 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1166 pMsg->message, pMsg->wParam, pMsg->lParam );
1169 NEXTHOOK:
1170 if ( pOleMenuDescriptor )
1171 GlobalUnlock( hOleMenu );
1173 /* Lookup the hook item for the current thread */
1174 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId(), NULL ) ) )
1176 /* This should never fail!! */
1177 WARN(ole, "could not retrieve hHook for current thread!\n" );
1178 return 0;
1181 /* Pass on the message to the next hooker */
1182 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1185 /*************************************************************************
1186 * OLEMenu_GetMsgProc
1187 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1188 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1190 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1192 LPMSG pMsg = NULL;
1193 HOLEMENU hOleMenu = 0;
1194 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1195 OleMenuHookItem *pHookItem = NULL;
1196 WORD wCode;
1198 TRACE(ole,"%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1200 /* Check if we're being asked to process a messages */
1201 if ( HC_ACTION != code )
1202 goto NEXTHOOK;
1204 /* Retrieve the current message being dispatched from lParam */
1205 pMsg = (LPMSG)lParam;
1207 /* Check if the message is destined for a window we are interested in:
1208 * If the window has an OLEMenu property we may need to dispatch
1209 * the menu message to its active objects window instead. */
1211 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1212 if ( !hOleMenu )
1213 goto NEXTHOOK;
1215 /* Process menu messages */
1216 switch( pMsg->message )
1218 case WM_COMMAND:
1220 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1221 if ( wCode )
1222 goto NEXTHOOK; /* Not a menu message */
1223 break;
1225 default:
1226 goto NEXTHOOK;
1229 /* Get the menu descriptor */
1230 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1231 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1232 goto NEXTHOOK;
1234 /* If the message was for the server dispatch it accordingly */
1235 if ( pOleMenuDescriptor->bIsServerItem )
1237 /* Change the hWnd in the message to the active objects hWnd.
1238 * The message loop which reads this message will automatically
1239 * dispatch it to the embedded objects window. */
1240 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1243 NEXTHOOK:
1244 if ( pOleMenuDescriptor )
1245 GlobalUnlock( hOleMenu );
1247 /* Lookup the hook item for the current thread */
1248 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId(), NULL ) ) )
1250 /* This should never fail!! */
1251 WARN(ole, "could not retrieve hHook for current thread!\n" );
1252 return FALSE;
1255 /* Pass on the message to the next hooker */
1256 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1259 /***********************************************************************
1260 * OleCreateMenuDescriptor [OLE32.97]
1261 * Creates an OLE menu descriptor for OLE to use when dispatching
1262 * menu messages and commands.
1264 * PARAMS:
1265 * hmenuCombined - Handle to the objects combined menu
1266 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1269 HOLEMENU WINAPI OleCreateMenuDescriptor(
1270 HMENU hmenuCombined,
1271 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1273 HOLEMENU hOleMenu;
1274 OleMenuDescriptor *pOleMenuDescriptor;
1275 int i;
1277 if ( !hmenuCombined || !lpMenuWidths )
1278 return 0;
1280 /* Create an OLE menu descriptor */
1281 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1282 sizeof(OleMenuDescriptor) ) ) )
1283 return 0;
1285 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1286 if ( !pOleMenuDescriptor )
1287 return 0;
1289 /* Initialize menu group widths and hmenu */
1290 for ( i = 0; i < 6; i++ )
1291 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1293 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1294 pOleMenuDescriptor->bIsServerItem = FALSE;
1295 GlobalUnlock( hOleMenu );
1297 return hOleMenu;
1300 /***********************************************************************
1301 * OleDestroyMenuDescriptor [OLE32.99]
1302 * Destroy the shared menu descriptor
1304 HRESULT WINAPI OleDestroyMenuDescriptor(
1305 HOLEMENU hmenuDescriptor)
1307 if ( hmenuDescriptor )
1308 GlobalFree( hmenuDescriptor );
1309 return S_OK;
1312 /***********************************************************************
1313 * OleSetMenuDescriptor [OLE32.129]
1314 * Installs or removes OLE dispatching code for the containers frame window
1315 * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1316 * OLE should install context sensitive help F1 filtering for the app when
1317 * these are non null.
1319 * PARAMS:
1320 * hOleMenu Handle to composite menu descriptor
1321 * hwndFrame Handle to containers frame window
1322 * hwndActiveObject Handle to objects in-place activation window
1323 * lpFrame Pointer to IOleInPlaceFrame on containers window
1324 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1326 * RETURNS:
1327 * S_OK - menu installed correctly
1328 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1330 HRESULT WINAPI OleSetMenuDescriptor(
1331 HOLEMENU hOleMenu,
1332 HWND hwndFrame,
1333 HWND hwndActiveObject,
1334 LPOLEINPLACEFRAME lpFrame,
1335 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1337 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1339 /* Check args */
1340 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1341 return E_INVALIDARG;
1343 if ( lpFrame || lpActiveObject )
1345 FIXME(ole,"(%x, %x, %x, %p, %p), Context sensitive help filtering not implemented!\n",
1346 (unsigned int)hOleMenu,
1347 hwndFrame,
1348 hwndActiveObject,
1349 lpFrame,
1350 lpActiveObject);
1353 /* Set up a message hook to intercept the containers frame window messages.
1354 * The message filter is responsible for dispatching menu messages from the
1355 * shared menu which are intended for the object.
1358 if ( hOleMenu ) /* Want to install dispatching code */
1360 /* If OLEMenu hooks are already installed for this thread, fail
1361 * Note: This effectively means that OleSetMenuDescriptor cannot
1362 * be called twice in succession on the same frame window
1363 * without first calling it with a null hOleMenu to uninstall */
1364 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId(), NULL ) )
1365 return E_FAIL;
1367 /* Get the menu descriptor */
1368 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1369 if ( !pOleMenuDescriptor )
1370 return E_UNEXPECTED;
1372 /* Update the menu descriptor */
1373 pOleMenuDescriptor->hwndFrame = hwndFrame;
1374 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1376 GlobalUnlock( hOleMenu );
1377 pOleMenuDescriptor = NULL;
1379 /* Add a menu descriptor windows property to the frame window */
1380 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1382 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1383 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1384 return E_FAIL;
1386 else /* Want to uninstall dispatching code */
1388 /* Uninstall the hooks */
1389 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1390 return E_FAIL;
1392 /* Remove the menu descriptor property from the frame window */
1393 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1396 return S_OK;
1399 /***********************************************************************
1400 * ReleaseStgMedium [OLE32.140]
1402 void WINAPI ReleaseStgMedium(
1403 STGMEDIUM* pmedium)
1405 switch (pmedium->tymed)
1407 case TYMED_HGLOBAL:
1409 if ( (pmedium->pUnkForRelease==0) &&
1410 (pmedium->u.hGlobal!=0) )
1411 GlobalFree(pmedium->u.hGlobal);
1413 pmedium->u.hGlobal = 0;
1414 break;
1416 case TYMED_FILE:
1418 if (pmedium->u.lpszFileName!=0)
1420 if (pmedium->pUnkForRelease==0)
1422 DeleteFileW(pmedium->u.lpszFileName);
1425 CoTaskMemFree(pmedium->u.lpszFileName);
1428 pmedium->u.lpszFileName = 0;
1429 break;
1431 case TYMED_ISTREAM:
1433 if (pmedium->u.pstm!=0)
1435 IStream_Release(pmedium->u.pstm);
1438 pmedium->u.pstm = 0;
1439 break;
1441 case TYMED_ISTORAGE:
1443 if (pmedium->u.pstg!=0)
1445 IStorage_Release(pmedium->u.pstg);
1448 pmedium->u.pstg = 0;
1449 break;
1451 case TYMED_GDI:
1453 if ( (pmedium->pUnkForRelease==0) &&
1454 (pmedium->u.hGlobal!=0) )
1455 DeleteObject(pmedium->u.hGlobal);
1457 pmedium->u.hGlobal = 0;
1458 break;
1460 case TYMED_MFPICT:
1462 if ( (pmedium->pUnkForRelease==0) &&
1463 (pmedium->u.hMetaFilePict!=0) )
1465 DeleteMetaFile(pmedium->u.hMetaFilePict);
1466 GlobalFree(pmedium->u.hMetaFilePict);
1469 pmedium->u.hMetaFilePict = 0;
1470 break;
1472 case TYMED_ENHMF:
1474 if ( (pmedium->pUnkForRelease==0) &&
1475 (pmedium->u.hEnhMetaFile!=0) )
1477 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1480 pmedium->u.hEnhMetaFile = 0;
1481 break;
1483 case TYMED_NULL:
1484 default:
1485 break;
1489 * After cleaning up, the unknown is released
1491 if (pmedium->pUnkForRelease!=0)
1493 IUnknown_Release(pmedium->pUnkForRelease);
1494 pmedium->pUnkForRelease = 0;
1498 /***
1499 * OLEDD_Initialize()
1501 * Initializes the OLE drag and drop data structures.
1503 static void OLEDD_Initialize()
1505 WNDCLASSA wndClass;
1507 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1508 wndClass.style = CS_GLOBALCLASS;
1509 wndClass.lpfnWndProc = (WNDPROC)OLEDD_DragTrackerWindowProc;
1510 wndClass.cbClsExtra = 0;
1511 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1512 wndClass.hCursor = 0;
1513 wndClass.hbrBackground = 0;
1514 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1516 RegisterClassA (&wndClass);
1519 /***
1520 * OLEDD_UnInitialize()
1522 * Releases the OLE drag and drop data structures.
1524 static void OLEDD_UnInitialize()
1527 * Simply empty the list.
1529 while (targetListHead!=NULL)
1531 RevokeDragDrop(targetListHead->hwndTarget);
1535 /***
1536 * OLEDD_InsertDropTarget()
1538 * Insert the target node in the tree.
1540 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1542 DropTargetNode* curNode;
1543 DropTargetNode** parentNodeLink;
1546 * Iterate the tree to find the insertion point.
1548 curNode = targetListHead;
1549 parentNodeLink = &targetListHead;
1551 while (curNode!=NULL)
1553 if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1556 * If the node we want to add has a smaller HWND, go left
1558 parentNodeLink = &curNode->prevDropTarget;
1559 curNode = curNode->prevDropTarget;
1561 else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1564 * If the node we want to add has a larger HWND, go right
1566 parentNodeLink = &curNode->nextDropTarget;
1567 curNode = curNode->nextDropTarget;
1569 else
1572 * The item was found in the list. It shouldn't have been there
1574 assert(FALSE);
1575 return;
1580 * If we get here, we have found a spot for our item. The parentNodeLink
1581 * pointer points to the pointer that we have to modify.
1582 * The curNode should be NULL. We just have to establish the link and Voila!
1584 assert(curNode==NULL);
1585 assert(parentNodeLink!=NULL);
1586 assert(*parentNodeLink==NULL);
1588 *parentNodeLink=nodeToAdd;
1591 /***
1592 * OLEDD_ExtractDropTarget()
1594 * Removes the target node from the tree.
1596 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1598 DropTargetNode* curNode;
1599 DropTargetNode** parentNodeLink;
1602 * Iterate the tree to find the insertion point.
1604 curNode = targetListHead;
1605 parentNodeLink = &targetListHead;
1607 while (curNode!=NULL)
1609 if (hwndOfTarget<curNode->hwndTarget)
1612 * If the node we want to add has a smaller HWND, go left
1614 parentNodeLink = &curNode->prevDropTarget;
1615 curNode = curNode->prevDropTarget;
1617 else if (hwndOfTarget>curNode->hwndTarget)
1620 * If the node we want to add has a larger HWND, go right
1622 parentNodeLink = &curNode->nextDropTarget;
1623 curNode = curNode->nextDropTarget;
1625 else
1628 * The item was found in the list. Detach it from it's parent and
1629 * re-insert it's kids in the tree.
1631 assert(parentNodeLink!=NULL);
1632 assert(*parentNodeLink==curNode);
1635 * We arbitrately re-attach the left sub-tree to the parent.
1637 *parentNodeLink = curNode->prevDropTarget;
1640 * And we re-insert the right subtree
1642 if (curNode->nextDropTarget!=NULL)
1644 OLEDD_InsertDropTarget(curNode->nextDropTarget);
1648 * The node we found is still a valid node once we complete
1649 * the unlinking of the kids.
1651 curNode->nextDropTarget=NULL;
1652 curNode->prevDropTarget=NULL;
1654 return curNode;
1659 * If we get here, the node is not in the tree
1661 return NULL;
1664 /***
1665 * OLEDD_FindDropTarget()
1667 * Finds information about the drop target.
1669 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1671 DropTargetNode* curNode;
1674 * Iterate the tree to find the HWND value.
1676 curNode = targetListHead;
1678 while (curNode!=NULL)
1680 if (hwndOfTarget<curNode->hwndTarget)
1683 * If the node we want to add has a smaller HWND, go left
1685 curNode = curNode->prevDropTarget;
1687 else if (hwndOfTarget>curNode->hwndTarget)
1690 * If the node we want to add has a larger HWND, go right
1692 curNode = curNode->nextDropTarget;
1694 else
1697 * The item was found in the list.
1699 return curNode;
1704 * If we get here, the item is not in the list
1706 return NULL;
1709 /***
1710 * OLEDD_DragTrackerWindowProc()
1712 * This method is the WindowProcedure of the drag n drop tracking
1713 * window. During a drag n Drop operation, an invisible window is created
1714 * to receive the user input and act upon it. This procedure is in charge
1715 * of this behavior.
1717 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1718 HWND hwnd,
1719 UINT uMsg,
1720 WPARAM wParam,
1721 LPARAM lParam)
1723 switch (uMsg)
1725 case WM_CREATE:
1727 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1729 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1732 break;
1734 case WM_MOUSEMOVE:
1736 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1737 POINT mousePos;
1740 * Get the current mouse position in screen coordinates.
1742 mousePos.x = LOWORD(lParam);
1743 mousePos.y = HIWORD(lParam);
1744 ClientToScreen(hwnd, &mousePos);
1747 * Track the movement of the mouse.
1749 OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1751 break;
1753 case WM_LBUTTONUP:
1754 case WM_MBUTTONUP:
1755 case WM_RBUTTONUP:
1756 case WM_LBUTTONDOWN:
1757 case WM_MBUTTONDOWN:
1758 case WM_RBUTTONDOWN:
1760 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1761 POINT mousePos;
1764 * Get the current mouse position in screen coordinates.
1766 mousePos.x = LOWORD(lParam);
1767 mousePos.y = HIWORD(lParam);
1768 ClientToScreen(hwnd, &mousePos);
1771 * Notify everyone that the button state changed
1772 * TODO: Check if the "escape" key was pressed.
1774 OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1776 break;
1781 * This is a window proc after all. Let's call the default.
1783 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1786 /***
1787 * OLEDD_TrackMouseMove()
1789 * This method is invoked while a drag and drop operation is in effect.
1790 * it will generate the appropriate callbacks in the drop source
1791 * and drop target. It will also provide the expected feedback to
1792 * the user.
1794 * params:
1795 * trackerInfo - Pointer to the structure identifying the
1796 * drag & drop operation that is currently
1797 * active.
1798 * mousePos - Current position of the mouse in screen
1799 * coordinates.
1800 * keyState - Contains the state of the shift keys and the
1801 * mouse buttons (MK_LBUTTON and the like)
1803 static void OLEDD_TrackMouseMove(
1804 TrackerWindowInfo* trackerInfo,
1805 POINT mousePos,
1806 DWORD keyState)
1808 HWND hwndNewTarget = 0;
1809 HRESULT hr = S_OK;
1812 * Get the handle of the window under the mouse
1814 hwndNewTarget = WindowFromPoint(mousePos);
1817 * Every time, we re-initialize the effects passed to the
1818 * IDropTarget to the effects allowed by the source.
1820 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1823 * If we are hovering over the same target as before, send the
1824 * DragOver notification
1826 if ( (trackerInfo->curDragTarget != 0) &&
1827 (trackerInfo->curDragTargetHWND==hwndNewTarget) )
1829 POINTL mousePosParam;
1832 * The documentation tells me that the coordinate should be in the target
1833 * window's coordinate space. However, the tests I made tell me the
1834 * coordinates should be in screen coordinates.
1836 mousePosParam.x = mousePos.x;
1837 mousePosParam.y = mousePos.y;
1839 IDropTarget_DragOver(trackerInfo->curDragTarget,
1840 keyState,
1841 mousePosParam,
1842 trackerInfo->pdwEffect);
1844 else
1846 DropTargetNode* newDropTargetNode = 0;
1849 * If we changed window, we have to notify our old target and check for
1850 * the new one.
1852 if (trackerInfo->curDragTarget!=0)
1854 IDropTarget_DragLeave(trackerInfo->curDragTarget);
1858 * Make sure we're hovering over a window.
1860 if (hwndNewTarget!=0)
1863 * Find-out if there is a drag target under the mouse
1865 newDropTargetNode = OLEDD_FindDropTarget(hwndNewTarget);
1867 trackerInfo->curDragTargetHWND = hwndNewTarget;
1868 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1871 * If there is, notify it that we just dragged-in
1873 if (trackerInfo->curDragTarget!=0)
1875 POINTL mousePosParam;
1878 * The documentation tells me that the coordinate should be in the target
1879 * window's coordinate space. However, the tests I made tell me the
1880 * coordinates should be in screen coordinates.
1882 mousePosParam.x = mousePos.x;
1883 mousePosParam.y = mousePos.y;
1885 IDropTarget_DragEnter(trackerInfo->curDragTarget,
1886 trackerInfo->dataObject,
1887 keyState,
1888 mousePosParam,
1889 trackerInfo->pdwEffect);
1892 else
1895 * The mouse is not over a window so we don't track anything.
1897 trackerInfo->curDragTargetHWND = 0;
1898 trackerInfo->curDragTarget = 0;
1903 * Now that we have done that, we have to tell the source to give
1904 * us feedback on the work being done by the target. If we don't
1905 * have a target, simulate no effect.
1907 if (trackerInfo->curDragTarget==0)
1909 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
1912 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
1913 *trackerInfo->pdwEffect);
1916 * When we ask for feedback from the drop source, sometimes it will
1917 * do all the necessary work and sometimes it will not handle it
1918 * when that's the case, we must display the standard drag and drop
1919 * cursors.
1921 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
1923 if ( (*trackerInfo->pdwEffect & DROPEFFECT_MOVE) ||
1924 (*trackerInfo->pdwEffect & DROPEFFECT_COPY) ||
1925 (*trackerInfo->pdwEffect & DROPEFFECT_LINK) )
1927 SetCursor(LoadCursorA(0, IDC_SIZEALLA));
1929 else
1931 SetCursor(LoadCursorA(0, IDC_NOA));
1936 /***
1937 * OLEDD_TrackStateChange()
1939 * This method is invoked while a drag and drop operation is in effect.
1940 * It is used to notify the drop target/drop source callbacks when
1941 * the state of the keyboard or mouse button change.
1943 * params:
1944 * trackerInfo - Pointer to the structure identifying the
1945 * drag & drop operation that is currently
1946 * active.
1947 * mousePos - Current position of the mouse in screen
1948 * coordinates.
1949 * keyState - Contains the state of the shift keys and the
1950 * mouse buttons (MK_LBUTTON and the like)
1952 static void OLEDD_TrackStateChange(
1953 TrackerWindowInfo* trackerInfo,
1954 POINT mousePos,
1955 DWORD keyState)
1958 * Ask the drop source what to do with the operation.
1960 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
1961 trackerInfo->dropSource,
1962 trackerInfo->escPressed,
1963 keyState);
1966 * All the return valued will stop the operation except the S_OK
1967 * return value.
1969 if (trackerInfo->returnValue!=S_OK)
1972 * Make sure the message loop in DoDragDrop stops
1974 trackerInfo->trackingDone = TRUE;
1977 * Release the mouse in case the drop target decides to show a popup
1978 * or a menu or something.
1980 ReleaseCapture();
1983 * If we end-up over a target, drop the object in the target or
1984 * inform the target that the operation was cancelled.
1986 if (trackerInfo->curDragTarget!=0)
1988 switch (trackerInfo->returnValue)
1991 * If the source wants us to complete the operation, we tell
1992 * the drop target that we just dropped the object in it.
1994 case DRAGDROP_S_DROP:
1996 POINTL mousePosParam;
1999 * The documentation tells me that the coordinate should be
2000 * in the target window's coordinate space. However, the tests
2001 * I made tell me the coordinates should be in screen coordinates.
2003 mousePosParam.x = mousePos.x;
2004 mousePosParam.y = mousePos.y;
2006 IDropTarget_Drop(trackerInfo->curDragTarget,
2007 trackerInfo->dataObject,
2008 keyState,
2009 mousePosParam,
2010 trackerInfo->pdwEffect);
2011 break;
2014 * If the source told us that we should cancel, fool the drop
2015 * target by telling it that the mouse left it's window.
2017 case DRAGDROP_S_CANCEL:
2018 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2019 break;
2025 /***
2026 * OLEDD_GetButtonState()
2028 * This method will use the current state of the keyboard to build
2029 * a button state mask equivalent to the one passed in the
2030 * WM_MOUSEMOVE wParam.
2032 static DWORD OLEDD_GetButtonState()
2034 BYTE keyboardState[256];
2035 DWORD keyMask = 0;
2037 GetKeyboardState(keyboardState);
2039 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2040 keyMask |= MK_SHIFT;
2042 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2043 keyMask |= MK_CONTROL;
2045 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2046 keyMask |= MK_LBUTTON;
2048 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2049 keyMask |= MK_RBUTTON;
2051 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2052 keyMask |= MK_MBUTTON;
2054 return keyMask;
2057 /***
2058 * OLEDD_GetButtonState()
2060 * This method will read the default value of the registry key in
2061 * parameter and extract a DWORD value from it. The registry key value
2062 * can be in a string key or a DWORD key.
2064 * params:
2065 * regKey - Key to read the default value from
2066 * pdwValue - Pointer to the location where the DWORD
2067 * value is returned. This value is not modified
2068 * if the value is not found.
2071 static void OLEUTL_ReadRegistryDWORDValue(
2072 HKEY regKey,
2073 DWORD* pdwValue)
2075 char buffer[20];
2076 DWORD dwKeyType;
2077 DWORD cbData = 20;
2078 LONG lres;
2080 lres = RegQueryValueExA(regKey,
2082 NULL,
2083 &dwKeyType,
2084 (LPBYTE)buffer,
2085 &cbData);
2087 if (lres==ERROR_SUCCESS)
2089 switch (dwKeyType)
2091 case REG_DWORD:
2092 *pdwValue = *(DWORD*)buffer;
2093 break;
2094 case REG_EXPAND_SZ:
2095 case REG_MULTI_SZ:
2096 case REG_SZ:
2097 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2098 break;