Implemented SHCreateShellFolderViewEx.
[wine.git] / ole / ole2.c
blob216ce4d37bc425d6321ef6903107fdf6c2d9e918
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 <string.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 "wine/wingdi16.h"
21 #include "debugtools.h"
22 #include "ole2ver.h"
23 #include "winreg.h"
25 DEFAULT_DEBUG_CHANNEL(ole)
27 /******************************************************************************
28 * These are static/global variables and internal data structures that the
29 * OLE module uses to maintain it's state.
31 typedef struct tagDropTargetNode
33 HWND hwndTarget;
34 IDropTarget* dropTarget;
35 struct tagDropTargetNode* prevDropTarget;
36 struct tagDropTargetNode* nextDropTarget;
37 } DropTargetNode;
39 typedef struct tagTrackerWindowInfo
41 IDataObject* dataObject;
42 IDropSource* dropSource;
43 DWORD dwOKEffect;
44 DWORD* pdwEffect;
45 BOOL trackingDone;
46 HRESULT returnValue;
48 BOOL escPressed;
49 HWND curDragTargetHWND;
50 IDropTarget* curDragTarget;
51 } TrackerWindowInfo;
53 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
55 HWND hwndFrame; /* The containers frame window */
56 HWND hwndActiveObject; /* The active objects window */
57 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
58 HMENU hmenuCombined; /* The combined menu */
59 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
60 } OleMenuDescriptor;
62 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
64 DWORD tid; /* Thread Id */
65 HANDLE hHeap; /* Heap this is allocated from */
66 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
67 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
68 } OleMenuHookItem;
71 * Dynamic pointer array of per thread message hooks (maintained by OleSetMenuDescriptor)
73 static HDPA OLEMenu_MsgHookDPA = NULL;
76 * This is the lock count on the OLE library. It is controlled by the
77 * OLEInitialize/OLEUninitialize methods.
79 static ULONG OLE_moduleLockCount = 0;
82 * Name of our registered window class.
84 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
87 * This is the head of the Drop target container.
89 static DropTargetNode* targetListHead = NULL;
91 /******************************************************************************
92 * These are the prototypes of miscelaneous utility methods
94 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
96 /******************************************************************************
97 * These are the prototypes of the utility methods used to manage a shared menu
99 static void OLEMenu_Initialize();
100 static void OLEMenu_UnInitialize();
101 BOOL OLEMenu_InstallHooks( DWORD tid );
102 BOOL OLEMenu_UnInstallHooks( DWORD tid );
103 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid, INT *pixHook );
104 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
105 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
106 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
107 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
109 /******************************************************************************
110 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
112 void OLEClipbrd_UnInitialize();
113 void OLEClipbrd_Initialize();
115 /******************************************************************************
116 * These are the prototypes of the utility methods used for OLE Drag n Drop
118 static void OLEDD_Initialize();
119 static void OLEDD_UnInitialize();
120 static void OLEDD_InsertDropTarget(
121 DropTargetNode* nodeToAdd);
122 static DropTargetNode* OLEDD_ExtractDropTarget(
123 HWND hwndOfTarget);
124 static DropTargetNode* OLEDD_FindDropTarget(
125 HWND hwndOfTarget);
126 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
127 HWND hwnd,
128 UINT uMsg,
129 WPARAM wParam,
130 LPARAM lParam);
131 static void OLEDD_TrackMouseMove(
132 TrackerWindowInfo* trackerInfo,
133 POINT mousePos,
134 DWORD keyState);
135 static void OLEDD_TrackStateChange(
136 TrackerWindowInfo* trackerInfo,
137 POINT mousePos,
138 DWORD keyState);
139 static DWORD OLEDD_GetButtonState();
142 /******************************************************************************
143 * OleBuildVersion [OLE2.1]
145 DWORD WINAPI OleBuildVersion(void)
147 TRACE("(void)\n");
148 return (rmm<<16)+rup;
151 /***********************************************************************
152 * OleInitialize (OLE2.2) (OLE32.108)
154 HRESULT WINAPI OleInitialize(LPVOID reserved)
156 HRESULT hr;
158 TRACE("(%p)\n", reserved);
161 * The first duty of the OleInitialize is to initialize the COM libraries.
163 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
166 * If the CoInitializeEx call failed, the OLE libraries can't be
167 * initialized.
169 if (FAILED(hr))
170 return hr;
173 * Then, it has to initialize the OLE specific modules.
174 * This includes:
175 * Clipboard
176 * Drag and Drop
177 * Object linking and Embedding
178 * In-place activation
180 if (OLE_moduleLockCount==0)
183 * Initialize the libraries.
185 TRACE("() - Initializing the OLE libraries\n");
188 * OLE Clipboard
190 OLEClipbrd_Initialize();
193 * Drag and Drop
195 OLEDD_Initialize();
198 * OLE shared menu
200 OLEMenu_Initialize();
204 * Then, we increase the lock count on the OLE module.
206 OLE_moduleLockCount++;
208 return hr;
211 /******************************************************************************
212 * CoGetCurrentProcess [COMPOBJ.34] [OLE2.2][OLE32.108]
214 * NOTES
215 * Is DWORD really the correct return type for this function?
217 DWORD WINAPI CoGetCurrentProcess(void) {
218 return (DWORD)PROCESS_Current();
221 /******************************************************************************
222 * OleUninitialize [OLE2.3] [OLE32.131]
224 void WINAPI OleUninitialize(void)
226 TRACE("()\n");
229 * Decrease the lock count on the OLE module.
231 OLE_moduleLockCount--;
234 * If we hit the bottom of the lock stack, free the libraries.
236 if (OLE_moduleLockCount==0)
239 * Actually free the libraries.
241 TRACE("() - Freeing the last reference count\n");
244 * OLE Clipboard
246 OLEClipbrd_UnInitialize();
249 * Drag and Drop
251 OLEDD_UnInitialize();
254 * OLE shared menu
256 OLEMenu_UnInitialize();
260 * Then, uninitialize the COM libraries.
262 CoUninitialize();
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("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("(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("(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("(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("(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("(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("(%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("(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("(%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("(%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("(%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("(%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("(%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 * Internal methods to manage the shared OLE menu in response to the
816 * OLE***MenuDescriptor API
819 /***
820 * OLEMenu_Initialize()
822 * Initializes the OLEMENU data structures.
824 static void OLEMenu_Initialize()
826 /* Create a dynamic pointer array to store the hook handles */
827 if ( !OLEMenu_MsgHookDPA )
828 OLEMenu_MsgHookDPA = DPA_CreateEx( 2, GetProcessHeap() );
831 /***
832 * OLEMenu_UnInitialize()
834 * Releases the OLEMENU data structures.
836 static void OLEMenu_UnInitialize()
838 /* Release the hook table */
839 if ( OLEMenu_MsgHookDPA )
840 DPA_Destroy( OLEMenu_MsgHookDPA );
842 OLEMenu_MsgHookDPA = NULL;
845 /*************************************************************************
846 * OLEMenu_InstallHooks
847 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
849 * RETURNS: TRUE if message hooks were succesfully installed
850 * FALSE on failure
852 BOOL OLEMenu_InstallHooks( DWORD tid )
854 OleMenuHookItem *pHookItem = NULL;
856 if ( !OLEMenu_MsgHookDPA ) /* No hook table? Create one */
858 /* Create a dynamic pointer array to store the hook handles */
859 if ( !(OLEMenu_MsgHookDPA = DPA_CreateEx( 2, GetProcessHeap() )) )
860 return FALSE;
863 /* Create an entry for the hook table */
864 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
865 sizeof(OleMenuHookItem)) ) )
866 return FALSE;
868 pHookItem->tid = tid;
869 pHookItem->hHeap = GetProcessHeap();
871 /* Install a thread scope message hook for WH_GETMESSAGE */
872 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
873 0, GetCurrentThreadId() );
874 if ( !pHookItem->GetMsg_hHook )
875 goto CLEANUP;
877 /* Install a thread scope message hook for WH_CALLWNDPROC */
878 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
879 0, GetCurrentThreadId() );
880 if ( !pHookItem->CallWndProc_hHook )
881 goto CLEANUP;
883 /* Insert the hook table entry */
884 if ( -1 == DPA_InsertPtr( OLEMenu_MsgHookDPA, 0, pHookItem ) )
885 goto CLEANUP;
887 return TRUE;
889 CLEANUP:
890 /* Unhook any hooks */
891 if ( pHookItem->GetMsg_hHook )
892 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
893 if ( pHookItem->CallWndProc_hHook )
894 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
895 /* Release the hook table entry */
896 HeapFree(pHookItem->hHeap, 0, pHookItem );
898 return FALSE;
901 /*************************************************************************
902 * OLEMenu_UnInstallHooks
903 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
905 * RETURNS: TRUE if message hooks were succesfully installed
906 * FALSE on failure
908 BOOL OLEMenu_UnInstallHooks( DWORD tid )
910 INT ixHook;
911 OleMenuHookItem *pHookItem = NULL;
913 if ( !OLEMenu_MsgHookDPA ) /* No hooks set */
914 return TRUE;
916 /* Lookup the hHook index for this tid */
917 if ( !OLEMenu_IsHookInstalled( tid , &ixHook ) )
918 return TRUE;
920 /* Remove the hook entry from the table(the pointer itself is not deleted) */
921 if ( !( pHookItem = DPA_DeletePtr(OLEMenu_MsgHookDPA, ixHook) ) )
922 return FALSE;
924 /* Uninstall the hooks installed for this thread */
925 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
926 goto CLEANUP;
927 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
928 goto CLEANUP;
930 /* Release the hook table entry */
931 HeapFree(pHookItem->hHeap, 0, pHookItem );
933 return TRUE;
935 CLEANUP:
936 /* Release the hook table entry */
937 if (pHookItem)
938 HeapFree(pHookItem->hHeap, 0, pHookItem );
940 return FALSE;
943 /*************************************************************************
944 * OLEMenu_IsHookInstalled
945 * Tests if OLEMenu hooks have been installed for a thread
947 * RETURNS: The pointer and index of the hook table entry for the tid
948 * NULL and -1 for the index if no hooks were installed for this thread
950 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid, INT *pixHook )
952 INT ixHook;
953 OleMenuHookItem *pHookItem = NULL;
955 if ( pixHook )
956 *pixHook = -1;
958 if ( !OLEMenu_MsgHookDPA ) /* No hooks set */
959 return NULL;
961 /* Do a simple linear search for an entry whose tid matches ours.
962 * We really need a map but efficiency is not a concern here. */
963 for( ixHook = 0; ; ixHook++ )
965 /* Retrieve the hook entry */
966 if ( !( pHookItem = DPA_GetPtr(OLEMenu_MsgHookDPA, ixHook) ) )
967 return NULL;
969 if ( tid == pHookItem->tid )
971 if ( pixHook )
972 *pixHook = ixHook;
973 return pHookItem;
977 return NULL;
980 /***********************************************************************
981 * OLEMenu_FindMainMenuIndex
983 * Used by OLEMenu API to find the top level group a menu item belongs to.
984 * On success pnPos contains the index of the item in the top level menu group
986 * RETURNS: TRUE if the ID was found, FALSE on failure
988 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
990 UINT i, nItems;
992 nItems = GetMenuItemCount( hMainMenu );
994 for (i = 0; i < nItems; i++)
996 HMENU hsubmenu;
998 /* Is the current item a submenu? */
999 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1001 /* If the handle is the same we're done */
1002 if ( hsubmenu == hPopupMenu )
1004 if (pnPos)
1005 *pnPos = i;
1006 return TRUE;
1008 /* Recursively search without updating pnPos */
1009 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1011 if (pnPos)
1012 *pnPos = i;
1013 return TRUE;
1018 return FALSE;
1021 /***********************************************************************
1022 * OLEMenu_SetIsServerMenu
1024 * Checks whether a popup menu belongs to a shared menu group which is
1025 * owned by the server, and sets the menu descriptor state accordingly.
1026 * All menu messages from these groups should be routed to the server.
1028 * RETURNS: TRUE if the popup menu is part of a server owned group
1029 * FASE if the popup menu is part of a container owned group
1031 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1033 UINT nPos = 0, nWidth, i;
1035 pOleMenuDescriptor->bIsServerItem = FALSE;
1037 /* Don't bother searching if the popup is the combined menu itself */
1038 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1039 return FALSE;
1041 /* Find the menu item index in the shared OLE menu that this item belongs to */
1042 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1043 return FALSE;
1045 /* The group widths array has counts for the number of elements
1046 * in the groups File, Edit, Container, Object, Window, Help.
1047 * The Edit, Object & Help groups belong to the server object
1048 * and the other three belong to the container.
1049 * Loop thru the group widths and locate the group we are a member of.
1051 for ( i = 0, nWidth = 0; i < 6; i++ )
1053 nWidth += pOleMenuDescriptor->mgw.width[i];
1054 if ( nPos < nWidth )
1056 /* Odd elements are server menu widths */
1057 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1058 break;
1062 return pOleMenuDescriptor->bIsServerItem;
1065 /*************************************************************************
1066 * OLEMenu_CallWndProc
1067 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1068 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1070 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1072 LPCWPSTRUCT pMsg = NULL;
1073 HOLEMENU hOleMenu = 0;
1074 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1075 OleMenuHookItem *pHookItem = NULL;
1076 WORD fuFlags;
1078 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1080 /* Check if we're being asked to process the message */
1081 if ( HC_ACTION != code )
1082 goto NEXTHOOK;
1084 /* Retrieve the current message being dispatched from lParam */
1085 pMsg = (LPCWPSTRUCT)lParam;
1087 /* Check if the message is destined for a window we are interested in:
1088 * If the window has an OLEMenu property we may need to dispatch
1089 * the menu message to its active objects window instead. */
1091 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1092 if ( !hOleMenu )
1093 goto NEXTHOOK;
1095 /* Get the menu descriptor */
1096 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1097 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1098 goto NEXTHOOK;
1100 /* Process menu messages */
1101 switch( pMsg->message )
1103 case WM_INITMENU:
1105 /* Reset the menu descriptor state */
1106 pOleMenuDescriptor->bIsServerItem = FALSE;
1108 /* Send this message to the server as well */
1109 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1110 pMsg->message, pMsg->wParam, pMsg->lParam );
1111 goto NEXTHOOK;
1114 case WM_INITMENUPOPUP:
1116 /* Save the state for whether this is a server owned menu */
1117 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1118 break;
1121 case WM_MENUSELECT:
1123 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1124 if ( fuFlags & MF_SYSMENU )
1125 goto NEXTHOOK;
1127 /* Save the state for whether this is a server owned popup menu */
1128 else if ( fuFlags & MF_POPUP )
1129 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1131 break;
1134 case WM_DRAWITEM:
1136 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1137 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1138 goto NEXTHOOK; /* Not a menu message */
1140 break;
1143 default:
1144 goto NEXTHOOK;
1147 /* If the message was for the server dispatch it accordingly */
1148 if ( pOleMenuDescriptor->bIsServerItem )
1150 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1151 pMsg->message, pMsg->wParam, pMsg->lParam );
1154 NEXTHOOK:
1155 if ( pOleMenuDescriptor )
1156 GlobalUnlock( hOleMenu );
1158 /* Lookup the hook item for the current thread */
1159 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId(), NULL ) ) )
1161 /* This should never fail!! */
1162 WARN("could not retrieve hHook for current thread!\n" );
1163 return 0;
1166 /* Pass on the message to the next hooker */
1167 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1170 /*************************************************************************
1171 * OLEMenu_GetMsgProc
1172 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1173 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1175 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1177 LPMSG pMsg = NULL;
1178 HOLEMENU hOleMenu = 0;
1179 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1180 OleMenuHookItem *pHookItem = NULL;
1181 WORD wCode;
1183 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1185 /* Check if we're being asked to process a messages */
1186 if ( HC_ACTION != code )
1187 goto NEXTHOOK;
1189 /* Retrieve the current message being dispatched from lParam */
1190 pMsg = (LPMSG)lParam;
1192 /* Check if the message is destined for a window we are interested in:
1193 * If the window has an OLEMenu property we may need to dispatch
1194 * the menu message to its active objects window instead. */
1196 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1197 if ( !hOleMenu )
1198 goto NEXTHOOK;
1200 /* Process menu messages */
1201 switch( pMsg->message )
1203 case WM_COMMAND:
1205 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1206 if ( wCode )
1207 goto NEXTHOOK; /* Not a menu message */
1208 break;
1210 default:
1211 goto NEXTHOOK;
1214 /* Get the menu descriptor */
1215 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1216 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1217 goto NEXTHOOK;
1219 /* If the message was for the server dispatch it accordingly */
1220 if ( pOleMenuDescriptor->bIsServerItem )
1222 /* Change the hWnd in the message to the active objects hWnd.
1223 * The message loop which reads this message will automatically
1224 * dispatch it to the embedded objects window. */
1225 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1228 NEXTHOOK:
1229 if ( pOleMenuDescriptor )
1230 GlobalUnlock( hOleMenu );
1232 /* Lookup the hook item for the current thread */
1233 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId(), NULL ) ) )
1235 /* This should never fail!! */
1236 WARN("could not retrieve hHook for current thread!\n" );
1237 return FALSE;
1240 /* Pass on the message to the next hooker */
1241 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1244 /***********************************************************************
1245 * OleCreateMenuDescriptor [OLE32.97]
1246 * Creates an OLE menu descriptor for OLE to use when dispatching
1247 * menu messages and commands.
1249 * PARAMS:
1250 * hmenuCombined - Handle to the objects combined menu
1251 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1254 HOLEMENU WINAPI OleCreateMenuDescriptor(
1255 HMENU hmenuCombined,
1256 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1258 HOLEMENU hOleMenu;
1259 OleMenuDescriptor *pOleMenuDescriptor;
1260 int i;
1262 if ( !hmenuCombined || !lpMenuWidths )
1263 return 0;
1265 /* Create an OLE menu descriptor */
1266 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1267 sizeof(OleMenuDescriptor) ) ) )
1268 return 0;
1270 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1271 if ( !pOleMenuDescriptor )
1272 return 0;
1274 /* Initialize menu group widths and hmenu */
1275 for ( i = 0; i < 6; i++ )
1276 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1278 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1279 pOleMenuDescriptor->bIsServerItem = FALSE;
1280 GlobalUnlock( hOleMenu );
1282 return hOleMenu;
1285 /***********************************************************************
1286 * OleDestroyMenuDescriptor [OLE32.99]
1287 * Destroy the shared menu descriptor
1289 HRESULT WINAPI OleDestroyMenuDescriptor(
1290 HOLEMENU hmenuDescriptor)
1292 if ( hmenuDescriptor )
1293 GlobalFree( hmenuDescriptor );
1294 return S_OK;
1297 /***********************************************************************
1298 * OleSetMenuDescriptor [OLE32.129]
1299 * Installs or removes OLE dispatching code for the containers frame window
1300 * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1301 * OLE should install context sensitive help F1 filtering for the app when
1302 * these are non null.
1304 * PARAMS:
1305 * hOleMenu Handle to composite menu descriptor
1306 * hwndFrame Handle to containers frame window
1307 * hwndActiveObject Handle to objects in-place activation window
1308 * lpFrame Pointer to IOleInPlaceFrame on containers window
1309 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1311 * RETURNS:
1312 * S_OK - menu installed correctly
1313 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1315 HRESULT WINAPI OleSetMenuDescriptor(
1316 HOLEMENU hOleMenu,
1317 HWND hwndFrame,
1318 HWND hwndActiveObject,
1319 LPOLEINPLACEFRAME lpFrame,
1320 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1322 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1324 /* Check args */
1325 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1326 return E_INVALIDARG;
1328 if ( lpFrame || lpActiveObject )
1330 FIXME("(%x, %x, %x, %p, %p), Context sensitive help filtering not implemented!\n",
1331 (unsigned int)hOleMenu,
1332 hwndFrame,
1333 hwndActiveObject,
1334 lpFrame,
1335 lpActiveObject);
1338 /* Set up a message hook to intercept the containers frame window messages.
1339 * The message filter is responsible for dispatching menu messages from the
1340 * shared menu which are intended for the object.
1343 if ( hOleMenu ) /* Want to install dispatching code */
1345 /* If OLEMenu hooks are already installed for this thread, fail
1346 * Note: This effectively means that OleSetMenuDescriptor cannot
1347 * be called twice in succession on the same frame window
1348 * without first calling it with a null hOleMenu to uninstall */
1349 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId(), NULL ) )
1350 return E_FAIL;
1352 /* Get the menu descriptor */
1353 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1354 if ( !pOleMenuDescriptor )
1355 return E_UNEXPECTED;
1357 /* Update the menu descriptor */
1358 pOleMenuDescriptor->hwndFrame = hwndFrame;
1359 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1361 GlobalUnlock( hOleMenu );
1362 pOleMenuDescriptor = NULL;
1364 /* Add a menu descriptor windows property to the frame window */
1365 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1367 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1368 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1369 return E_FAIL;
1371 else /* Want to uninstall dispatching code */
1373 /* Uninstall the hooks */
1374 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1375 return E_FAIL;
1377 /* Remove the menu descriptor property from the frame window */
1378 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1381 return S_OK;
1384 /***********************************************************************
1385 * ReleaseStgMedium [OLE32.140]
1387 void WINAPI ReleaseStgMedium(
1388 STGMEDIUM* pmedium)
1390 switch (pmedium->tymed)
1392 case TYMED_HGLOBAL:
1394 if ( (pmedium->pUnkForRelease==0) &&
1395 (pmedium->u.hGlobal!=0) )
1396 GlobalFree(pmedium->u.hGlobal);
1398 pmedium->u.hGlobal = 0;
1399 break;
1401 case TYMED_FILE:
1403 if (pmedium->u.lpszFileName!=0)
1405 if (pmedium->pUnkForRelease==0)
1407 DeleteFileW(pmedium->u.lpszFileName);
1410 CoTaskMemFree(pmedium->u.lpszFileName);
1413 pmedium->u.lpszFileName = 0;
1414 break;
1416 case TYMED_ISTREAM:
1418 if (pmedium->u.pstm!=0)
1420 IStream_Release(pmedium->u.pstm);
1423 pmedium->u.pstm = 0;
1424 break;
1426 case TYMED_ISTORAGE:
1428 if (pmedium->u.pstg!=0)
1430 IStorage_Release(pmedium->u.pstg);
1433 pmedium->u.pstg = 0;
1434 break;
1436 case TYMED_GDI:
1438 if ( (pmedium->pUnkForRelease==0) &&
1439 (pmedium->u.hGlobal!=0) )
1440 DeleteObject(pmedium->u.hGlobal);
1442 pmedium->u.hGlobal = 0;
1443 break;
1445 case TYMED_MFPICT:
1447 if ( (pmedium->pUnkForRelease==0) &&
1448 (pmedium->u.hMetaFilePict!=0) )
1450 DeleteMetaFile(pmedium->u.hMetaFilePict);
1451 GlobalFree(pmedium->u.hMetaFilePict);
1454 pmedium->u.hMetaFilePict = 0;
1455 break;
1457 case TYMED_ENHMF:
1459 if ( (pmedium->pUnkForRelease==0) &&
1460 (pmedium->u.hEnhMetaFile!=0) )
1462 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1465 pmedium->u.hEnhMetaFile = 0;
1466 break;
1468 case TYMED_NULL:
1469 default:
1470 break;
1474 * After cleaning up, the unknown is released
1476 if (pmedium->pUnkForRelease!=0)
1478 IUnknown_Release(pmedium->pUnkForRelease);
1479 pmedium->pUnkForRelease = 0;
1483 /***
1484 * OLEDD_Initialize()
1486 * Initializes the OLE drag and drop data structures.
1488 static void OLEDD_Initialize()
1490 WNDCLASSA wndClass;
1492 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1493 wndClass.style = CS_GLOBALCLASS;
1494 wndClass.lpfnWndProc = (WNDPROC)OLEDD_DragTrackerWindowProc;
1495 wndClass.cbClsExtra = 0;
1496 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1497 wndClass.hCursor = 0;
1498 wndClass.hbrBackground = 0;
1499 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1501 RegisterClassA (&wndClass);
1504 /***
1505 * OLEDD_UnInitialize()
1507 * Releases the OLE drag and drop data structures.
1509 static void OLEDD_UnInitialize()
1512 * Simply empty the list.
1514 while (targetListHead!=NULL)
1516 RevokeDragDrop(targetListHead->hwndTarget);
1520 /***
1521 * OLEDD_InsertDropTarget()
1523 * Insert the target node in the tree.
1525 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1527 DropTargetNode* curNode;
1528 DropTargetNode** parentNodeLink;
1531 * Iterate the tree to find the insertion point.
1533 curNode = targetListHead;
1534 parentNodeLink = &targetListHead;
1536 while (curNode!=NULL)
1538 if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1541 * If the node we want to add has a smaller HWND, go left
1543 parentNodeLink = &curNode->prevDropTarget;
1544 curNode = curNode->prevDropTarget;
1546 else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1549 * If the node we want to add has a larger HWND, go right
1551 parentNodeLink = &curNode->nextDropTarget;
1552 curNode = curNode->nextDropTarget;
1554 else
1557 * The item was found in the list. It shouldn't have been there
1559 assert(FALSE);
1560 return;
1565 * If we get here, we have found a spot for our item. The parentNodeLink
1566 * pointer points to the pointer that we have to modify.
1567 * The curNode should be NULL. We just have to establish the link and Voila!
1569 assert(curNode==NULL);
1570 assert(parentNodeLink!=NULL);
1571 assert(*parentNodeLink==NULL);
1573 *parentNodeLink=nodeToAdd;
1576 /***
1577 * OLEDD_ExtractDropTarget()
1579 * Removes the target node from the tree.
1581 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1583 DropTargetNode* curNode;
1584 DropTargetNode** parentNodeLink;
1587 * Iterate the tree to find the insertion point.
1589 curNode = targetListHead;
1590 parentNodeLink = &targetListHead;
1592 while (curNode!=NULL)
1594 if (hwndOfTarget<curNode->hwndTarget)
1597 * If the node we want to add has a smaller HWND, go left
1599 parentNodeLink = &curNode->prevDropTarget;
1600 curNode = curNode->prevDropTarget;
1602 else if (hwndOfTarget>curNode->hwndTarget)
1605 * If the node we want to add has a larger HWND, go right
1607 parentNodeLink = &curNode->nextDropTarget;
1608 curNode = curNode->nextDropTarget;
1610 else
1613 * The item was found in the list. Detach it from it's parent and
1614 * re-insert it's kids in the tree.
1616 assert(parentNodeLink!=NULL);
1617 assert(*parentNodeLink==curNode);
1620 * We arbitrately re-attach the left sub-tree to the parent.
1622 *parentNodeLink = curNode->prevDropTarget;
1625 * And we re-insert the right subtree
1627 if (curNode->nextDropTarget!=NULL)
1629 OLEDD_InsertDropTarget(curNode->nextDropTarget);
1633 * The node we found is still a valid node once we complete
1634 * the unlinking of the kids.
1636 curNode->nextDropTarget=NULL;
1637 curNode->prevDropTarget=NULL;
1639 return curNode;
1644 * If we get here, the node is not in the tree
1646 return NULL;
1649 /***
1650 * OLEDD_FindDropTarget()
1652 * Finds information about the drop target.
1654 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1656 DropTargetNode* curNode;
1659 * Iterate the tree to find the HWND value.
1661 curNode = targetListHead;
1663 while (curNode!=NULL)
1665 if (hwndOfTarget<curNode->hwndTarget)
1668 * If the node we want to add has a smaller HWND, go left
1670 curNode = curNode->prevDropTarget;
1672 else if (hwndOfTarget>curNode->hwndTarget)
1675 * If the node we want to add has a larger HWND, go right
1677 curNode = curNode->nextDropTarget;
1679 else
1682 * The item was found in the list.
1684 return curNode;
1689 * If we get here, the item is not in the list
1691 return NULL;
1694 /***
1695 * OLEDD_DragTrackerWindowProc()
1697 * This method is the WindowProcedure of the drag n drop tracking
1698 * window. During a drag n Drop operation, an invisible window is created
1699 * to receive the user input and act upon it. This procedure is in charge
1700 * of this behavior.
1702 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1703 HWND hwnd,
1704 UINT uMsg,
1705 WPARAM wParam,
1706 LPARAM lParam)
1708 switch (uMsg)
1710 case WM_CREATE:
1712 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1714 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1717 break;
1719 case WM_MOUSEMOVE:
1721 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1722 POINT mousePos;
1725 * Get the current mouse position in screen coordinates.
1727 mousePos.x = LOWORD(lParam);
1728 mousePos.y = HIWORD(lParam);
1729 ClientToScreen(hwnd, &mousePos);
1732 * Track the movement of the mouse.
1734 OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1736 break;
1738 case WM_LBUTTONUP:
1739 case WM_MBUTTONUP:
1740 case WM_RBUTTONUP:
1741 case WM_LBUTTONDOWN:
1742 case WM_MBUTTONDOWN:
1743 case WM_RBUTTONDOWN:
1745 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1746 POINT mousePos;
1749 * Get the current mouse position in screen coordinates.
1751 mousePos.x = LOWORD(lParam);
1752 mousePos.y = HIWORD(lParam);
1753 ClientToScreen(hwnd, &mousePos);
1756 * Notify everyone that the button state changed
1757 * TODO: Check if the "escape" key was pressed.
1759 OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1761 break;
1766 * This is a window proc after all. Let's call the default.
1768 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1771 /***
1772 * OLEDD_TrackMouseMove()
1774 * This method is invoked while a drag and drop operation is in effect.
1775 * it will generate the appropriate callbacks in the drop source
1776 * and drop target. It will also provide the expected feedback to
1777 * the user.
1779 * params:
1780 * trackerInfo - Pointer to the structure identifying the
1781 * drag & drop operation that is currently
1782 * active.
1783 * mousePos - Current position of the mouse in screen
1784 * coordinates.
1785 * keyState - Contains the state of the shift keys and the
1786 * mouse buttons (MK_LBUTTON and the like)
1788 static void OLEDD_TrackMouseMove(
1789 TrackerWindowInfo* trackerInfo,
1790 POINT mousePos,
1791 DWORD keyState)
1793 HWND hwndNewTarget = 0;
1794 HRESULT hr = S_OK;
1797 * Get the handle of the window under the mouse
1799 hwndNewTarget = WindowFromPoint(mousePos);
1802 * Every time, we re-initialize the effects passed to the
1803 * IDropTarget to the effects allowed by the source.
1805 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1808 * If we are hovering over the same target as before, send the
1809 * DragOver notification
1811 if ( (trackerInfo->curDragTarget != 0) &&
1812 (trackerInfo->curDragTargetHWND==hwndNewTarget) )
1814 POINTL mousePosParam;
1817 * The documentation tells me that the coordinate should be in the target
1818 * window's coordinate space. However, the tests I made tell me the
1819 * coordinates should be in screen coordinates.
1821 mousePosParam.x = mousePos.x;
1822 mousePosParam.y = mousePos.y;
1824 IDropTarget_DragOver(trackerInfo->curDragTarget,
1825 keyState,
1826 mousePosParam,
1827 trackerInfo->pdwEffect);
1829 else
1831 DropTargetNode* newDropTargetNode = 0;
1834 * If we changed window, we have to notify our old target and check for
1835 * the new one.
1837 if (trackerInfo->curDragTarget!=0)
1839 IDropTarget_DragLeave(trackerInfo->curDragTarget);
1843 * Make sure we're hovering over a window.
1845 if (hwndNewTarget!=0)
1848 * Find-out if there is a drag target under the mouse
1850 newDropTargetNode = OLEDD_FindDropTarget(hwndNewTarget);
1852 trackerInfo->curDragTargetHWND = hwndNewTarget;
1853 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1856 * If there is, notify it that we just dragged-in
1858 if (trackerInfo->curDragTarget!=0)
1860 POINTL mousePosParam;
1863 * The documentation tells me that the coordinate should be in the target
1864 * window's coordinate space. However, the tests I made tell me the
1865 * coordinates should be in screen coordinates.
1867 mousePosParam.x = mousePos.x;
1868 mousePosParam.y = mousePos.y;
1870 IDropTarget_DragEnter(trackerInfo->curDragTarget,
1871 trackerInfo->dataObject,
1872 keyState,
1873 mousePosParam,
1874 trackerInfo->pdwEffect);
1877 else
1880 * The mouse is not over a window so we don't track anything.
1882 trackerInfo->curDragTargetHWND = 0;
1883 trackerInfo->curDragTarget = 0;
1888 * Now that we have done that, we have to tell the source to give
1889 * us feedback on the work being done by the target. If we don't
1890 * have a target, simulate no effect.
1892 if (trackerInfo->curDragTarget==0)
1894 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
1897 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
1898 *trackerInfo->pdwEffect);
1901 * When we ask for feedback from the drop source, sometimes it will
1902 * do all the necessary work and sometimes it will not handle it
1903 * when that's the case, we must display the standard drag and drop
1904 * cursors.
1906 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
1908 if ( (*trackerInfo->pdwEffect & DROPEFFECT_MOVE) ||
1909 (*trackerInfo->pdwEffect & DROPEFFECT_COPY) ||
1910 (*trackerInfo->pdwEffect & DROPEFFECT_LINK) )
1912 SetCursor(LoadCursorA(0, IDC_SIZEALLA));
1914 else
1916 SetCursor(LoadCursorA(0, IDC_NOA));
1921 /***
1922 * OLEDD_TrackStateChange()
1924 * This method is invoked while a drag and drop operation is in effect.
1925 * It is used to notify the drop target/drop source callbacks when
1926 * the state of the keyboard or mouse button change.
1928 * params:
1929 * trackerInfo - Pointer to the structure identifying the
1930 * drag & drop operation that is currently
1931 * active.
1932 * mousePos - Current position of the mouse in screen
1933 * coordinates.
1934 * keyState - Contains the state of the shift keys and the
1935 * mouse buttons (MK_LBUTTON and the like)
1937 static void OLEDD_TrackStateChange(
1938 TrackerWindowInfo* trackerInfo,
1939 POINT mousePos,
1940 DWORD keyState)
1943 * Ask the drop source what to do with the operation.
1945 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
1946 trackerInfo->dropSource,
1947 trackerInfo->escPressed,
1948 keyState);
1951 * All the return valued will stop the operation except the S_OK
1952 * return value.
1954 if (trackerInfo->returnValue!=S_OK)
1957 * Make sure the message loop in DoDragDrop stops
1959 trackerInfo->trackingDone = TRUE;
1962 * Release the mouse in case the drop target decides to show a popup
1963 * or a menu or something.
1965 ReleaseCapture();
1968 * If we end-up over a target, drop the object in the target or
1969 * inform the target that the operation was cancelled.
1971 if (trackerInfo->curDragTarget!=0)
1973 switch (trackerInfo->returnValue)
1976 * If the source wants us to complete the operation, we tell
1977 * the drop target that we just dropped the object in it.
1979 case DRAGDROP_S_DROP:
1981 POINTL mousePosParam;
1984 * The documentation tells me that the coordinate should be
1985 * in the target window's coordinate space. However, the tests
1986 * I made tell me the coordinates should be in screen coordinates.
1988 mousePosParam.x = mousePos.x;
1989 mousePosParam.y = mousePos.y;
1991 IDropTarget_Drop(trackerInfo->curDragTarget,
1992 trackerInfo->dataObject,
1993 keyState,
1994 mousePosParam,
1995 trackerInfo->pdwEffect);
1996 break;
1999 * If the source told us that we should cancel, fool the drop
2000 * target by telling it that the mouse left it's window.
2002 case DRAGDROP_S_CANCEL:
2003 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2004 break;
2010 /***
2011 * OLEDD_GetButtonState()
2013 * This method will use the current state of the keyboard to build
2014 * a button state mask equivalent to the one passed in the
2015 * WM_MOUSEMOVE wParam.
2017 static DWORD OLEDD_GetButtonState()
2019 BYTE keyboardState[256];
2020 DWORD keyMask = 0;
2022 GetKeyboardState(keyboardState);
2024 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2025 keyMask |= MK_SHIFT;
2027 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2028 keyMask |= MK_CONTROL;
2030 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2031 keyMask |= MK_LBUTTON;
2033 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2034 keyMask |= MK_RBUTTON;
2036 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2037 keyMask |= MK_MBUTTON;
2039 return keyMask;
2042 /***
2043 * OLEDD_GetButtonState()
2045 * This method will read the default value of the registry key in
2046 * parameter and extract a DWORD value from it. The registry key value
2047 * can be in a string key or a DWORD key.
2049 * params:
2050 * regKey - Key to read the default value from
2051 * pdwValue - Pointer to the location where the DWORD
2052 * value is returned. This value is not modified
2053 * if the value is not found.
2056 static void OLEUTL_ReadRegistryDWORDValue(
2057 HKEY regKey,
2058 DWORD* pdwValue)
2060 char buffer[20];
2061 DWORD dwKeyType;
2062 DWORD cbData = 20;
2063 LONG lres;
2065 lres = RegQueryValueExA(regKey,
2067 NULL,
2068 &dwKeyType,
2069 (LPBYTE)buffer,
2070 &cbData);
2072 if (lres==ERROR_SUCCESS)
2074 switch (dwKeyType)
2076 case REG_DWORD:
2077 *pdwValue = *(DWORD*)buffer;
2078 break;
2079 case REG_EXPAND_SZ:
2080 case REG_MULTI_SZ:
2081 case REG_SZ:
2082 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2083 break;
2088 /******************************************************************************
2089 * OleMetaFilePictFromIconAndLabel
2091 * Returns a global memory handle to a metafile which contains the icon and
2092 * label given.
2093 * I guess the result of that should look somehow like desktop icons.
2094 * If no hIcon is given, we load the icon via lpszSourceFile and iIconIndex.
2095 * This code might be wrong at some places.
2097 HGLOBAL16 WINAPI OleMetaFilePictFromIconAndLabel16(
2098 HICON16 hIcon,
2099 LPCOLESTR16 lpszLabel,
2100 LPCOLESTR16 lpszSourceFile,
2101 UINT16 iIconIndex
2103 METAFILEPICT16 *mf;
2104 HGLOBAL16 hmf;
2105 HDC16 hdc;
2107 FIXME("(%04x, '%s', '%s', %d): incorrect metrics, please try to correct them !\n\n\n", hIcon, lpszLabel, lpszSourceFile, iIconIndex);
2109 if (!hIcon) {
2110 if (lpszSourceFile) {
2111 HINSTANCE16 hInstance = LoadLibrary16(lpszSourceFile);
2113 /* load the icon at index from lpszSourceFile */
2114 hIcon = (HICON16)LoadIconA(hInstance, (LPCSTR)(DWORD)iIconIndex);
2115 FreeLibrary16(hInstance);
2116 } else
2117 return (HGLOBAL)NULL;
2120 hdc = CreateMetaFile16(NULL);
2121 DrawIcon(hdc, 0, 0, hIcon); /* FIXME */
2122 TextOut16(hdc, 0, 0, lpszLabel, 1); /* FIXME */
2123 hmf = GlobalAlloc16(0, sizeof(METAFILEPICT16));
2124 mf = (METAFILEPICT16 *)GlobalLock16(hmf);
2125 mf->mm = MM_ANISOTROPIC;
2126 mf->xExt = 20; /* FIXME: bogus */
2127 mf->yExt = 20; /* dito */
2128 mf->hMF = CloseMetaFile16(hdc);
2129 return hmf;