Should initialize COM before calling it.
[wine/testsucceed.git] / dlls / ole32 / ole2.c
blob5cb736b9c187d3476801105fef66ef763ce5575a
2 /*
3 * OLE2 library
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Noel Borthwick
8 */
10 #include "config.h"
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
17 #include "commctrl.h"
18 #include "ole2.h"
19 #include "ole2ver.h"
20 #include "windef.h"
21 #include "winbase.h"
22 #include "winerror.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "winreg.h"
27 #include "wine/obj_clientserver.h"
28 #include "wine/winbase16.h"
29 #include "wine/wingdi16.h"
30 #include "wine/winuser16.h"
31 #include "ole32_main.h"
33 #include "debugtools.h"
35 DEFAULT_DEBUG_CHANNEL(ole);
36 DECLARE_DEBUG_CHANNEL(accel);
38 /******************************************************************************
39 * These are static/global variables and internal data structures that the
40 * OLE module uses to maintain it's state.
42 typedef struct tagDropTargetNode
44 HWND hwndTarget;
45 IDropTarget* dropTarget;
46 struct tagDropTargetNode* prevDropTarget;
47 struct tagDropTargetNode* nextDropTarget;
48 } DropTargetNode;
50 typedef struct tagTrackerWindowInfo
52 IDataObject* dataObject;
53 IDropSource* dropSource;
54 DWORD dwOKEffect;
55 DWORD* pdwEffect;
56 BOOL trackingDone;
57 HRESULT returnValue;
59 BOOL escPressed;
60 HWND curDragTargetHWND;
61 IDropTarget* curDragTarget;
62 } TrackerWindowInfo;
64 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
66 HWND hwndFrame; /* The containers frame window */
67 HWND hwndActiveObject; /* The active objects window */
68 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
69 HMENU hmenuCombined; /* The combined menu */
70 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
71 } OleMenuDescriptor;
73 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
75 DWORD tid; /* Thread Id */
76 HANDLE hHeap; /* Heap this is allocated from */
77 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
78 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
79 struct tagOleMenuHookItem *next;
80 } OleMenuHookItem;
82 static OleMenuHookItem *hook_list;
85 * This is the lock count on the OLE library. It is controlled by the
86 * OLEInitialize/OLEUninitialize methods.
88 static ULONG OLE_moduleLockCount = 0;
91 * Name of our registered window class.
93 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
96 * This is the head of the Drop target container.
98 static DropTargetNode* targetListHead = NULL;
100 /******************************************************************************
101 * These are the prototypes of miscelaneous utility methods
103 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
105 /******************************************************************************
106 * These are the prototypes of the utility methods used to manage a shared menu
108 static void OLEMenu_Initialize();
109 static void OLEMenu_UnInitialize();
110 BOOL OLEMenu_InstallHooks( DWORD tid );
111 BOOL OLEMenu_UnInstallHooks( DWORD tid );
112 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
113 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
114 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
115 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
116 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
118 /******************************************************************************
119 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
121 void OLEClipbrd_UnInitialize();
122 void OLEClipbrd_Initialize();
124 /******************************************************************************
125 * These are the prototypes of the utility methods used for OLE Drag n Drop
127 static void OLEDD_Initialize();
128 static void OLEDD_UnInitialize();
129 static void OLEDD_InsertDropTarget(
130 DropTargetNode* nodeToAdd);
131 static DropTargetNode* OLEDD_ExtractDropTarget(
132 HWND hwndOfTarget);
133 static DropTargetNode* OLEDD_FindDropTarget(
134 HWND hwndOfTarget);
135 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
136 HWND hwnd,
137 UINT uMsg,
138 WPARAM wParam,
139 LPARAM lParam);
140 static void OLEDD_TrackMouseMove(
141 TrackerWindowInfo* trackerInfo,
142 POINT mousePos,
143 DWORD keyState);
144 static void OLEDD_TrackStateChange(
145 TrackerWindowInfo* trackerInfo,
146 POINT mousePos,
147 DWORD keyState);
148 static DWORD OLEDD_GetButtonState();
151 /******************************************************************************
152 * OleBuildVersion [OLE2.1]
153 * OleBuildVersion [OLE32.84]
155 DWORD WINAPI OleBuildVersion(void)
157 TRACE("Returning version %d, build %d.\n", rmm, rup);
158 return (rmm<<16)+rup;
161 /***********************************************************************
162 * OleInitialize (OLE2.2)
163 * OleInitialize (OLE32.108)
165 HRESULT WINAPI OleInitialize(LPVOID reserved)
167 HRESULT hr;
169 TRACE("(%p)\n", reserved);
172 * The first duty of the OleInitialize is to initialize the COM libraries.
174 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
177 * If the CoInitializeEx call failed, the OLE libraries can't be
178 * initialized.
180 if (FAILED(hr))
181 return hr;
184 * Then, it has to initialize the OLE specific modules.
185 * This includes:
186 * Clipboard
187 * Drag and Drop
188 * Object linking and Embedding
189 * In-place activation
191 if (OLE_moduleLockCount==0)
194 * Initialize the libraries.
196 TRACE("() - Initializing the OLE libraries\n");
199 * OLE Clipboard
201 OLEClipbrd_Initialize();
204 * Drag and Drop
206 OLEDD_Initialize();
209 * OLE shared menu
211 OLEMenu_Initialize();
215 * Then, we increase the lock count on the OLE module.
217 OLE_moduleLockCount++;
219 return hr;
222 /******************************************************************************
223 * CoGetCurrentProcess [COMPOBJ.34]
224 * CoGetCurrentProcess [OLE32.18]
226 * NOTES
227 * Is DWORD really the correct return type for this function?
229 DWORD WINAPI CoGetCurrentProcess(void)
231 return GetCurrentProcessId();
234 /******************************************************************************
235 * OleUninitialize [OLE2.3]
236 * OleUninitialize [OLE32.131]
238 void WINAPI OleUninitialize(void)
240 TRACE("()\n");
243 * Decrease the lock count on the OLE module.
245 OLE_moduleLockCount--;
248 * If we hit the bottom of the lock stack, free the libraries.
250 if (OLE_moduleLockCount==0)
253 * Actually free the libraries.
255 TRACE("() - Freeing the last reference count\n");
258 * OLE Clipboard
260 OLEClipbrd_UnInitialize();
263 * Drag and Drop
265 OLEDD_UnInitialize();
268 * OLE shared menu
270 OLEMenu_UnInitialize();
274 * Then, uninitialize the COM libraries.
276 CoUninitialize();
279 /******************************************************************************
280 * CoRegisterMessageFilter [OLE32.38]
282 HRESULT WINAPI CoRegisterMessageFilter(
283 LPMESSAGEFILTER lpMessageFilter, /* [in] Pointer to interface */
284 LPMESSAGEFILTER *lplpMessageFilter /* [out] Indirect pointer to prior instance if non-NULL */
286 FIXME("stub\n");
287 if (lplpMessageFilter) {
288 *lplpMessageFilter = NULL;
290 return S_OK;
293 /******************************************************************************
294 * OleInitializeWOW [OLE32.109]
296 HRESULT WINAPI OleInitializeWOW(DWORD x) {
297 FIXME("(0x%08lx),stub!\n",x);
298 return 0;
301 /***********************************************************************
302 * RegisterDragDrop (OLE2.35)
304 HRESULT WINAPI RegisterDragDrop16(
305 HWND16 hwnd,
306 LPDROPTARGET pDropTarget
308 FIXME("(0x%04x,%p),stub!\n",hwnd,pDropTarget);
309 return S_OK;
312 /***********************************************************************
313 * RegisterDragDrop (OLE32.139)
315 HRESULT WINAPI RegisterDragDrop(
316 HWND hwnd,
317 LPDROPTARGET pDropTarget)
319 DropTargetNode* dropTargetInfo;
321 TRACE("(0x%x,%p)\n", hwnd, pDropTarget);
324 * First, check if the window is already registered.
326 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
328 if (dropTargetInfo!=NULL)
329 return DRAGDROP_E_ALREADYREGISTERED;
332 * If it's not there, we can add it. We first create a node for it.
334 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
336 if (dropTargetInfo==NULL)
337 return E_OUTOFMEMORY;
339 dropTargetInfo->hwndTarget = hwnd;
340 dropTargetInfo->prevDropTarget = NULL;
341 dropTargetInfo->nextDropTarget = NULL;
344 * Don't forget that this is an interface pointer, need to nail it down since
345 * we keep a copy of it.
347 dropTargetInfo->dropTarget = pDropTarget;
348 IDropTarget_AddRef(dropTargetInfo->dropTarget);
350 OLEDD_InsertDropTarget(dropTargetInfo);
352 return S_OK;
355 /***********************************************************************
356 * RevokeDragDrop (OLE2.36)
358 HRESULT WINAPI RevokeDragDrop16(
359 HWND16 hwnd
361 FIXME("(0x%04x),stub!\n",hwnd);
362 return S_OK;
365 /***********************************************************************
366 * RevokeDragDrop (OLE32.141)
368 HRESULT WINAPI RevokeDragDrop(
369 HWND hwnd)
371 DropTargetNode* dropTargetInfo;
373 TRACE("(0x%x)\n", hwnd);
376 * First, check if the window is already registered.
378 dropTargetInfo = OLEDD_ExtractDropTarget(hwnd);
381 * If it ain't in there, it's an error.
383 if (dropTargetInfo==NULL)
384 return DRAGDROP_E_NOTREGISTERED;
387 * If it's in there, clean-up it's used memory and
388 * references
390 IDropTarget_Release(dropTargetInfo->dropTarget);
391 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
393 return S_OK;
396 /***********************************************************************
397 * OleRegGetUserType (OLE32.122)
399 * This implementation of OleRegGetUserType ignores the dwFormOfType
400 * parameter and always returns the full name of the object. This is
401 * not too bad since this is the case for many objects because of the
402 * way they are registered.
404 HRESULT WINAPI OleRegGetUserType(
405 REFCLSID clsid,
406 DWORD dwFormOfType,
407 LPOLESTR* pszUserType)
409 char keyName[60];
410 DWORD dwKeyType;
411 DWORD cbData;
412 HKEY clsidKey;
413 LONG hres;
414 LPBYTE buffer;
415 HRESULT retVal;
417 * Initialize the out parameter.
419 *pszUserType = NULL;
422 * Build the key name we're looking for
424 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
425 clsid->Data1, clsid->Data2, clsid->Data3,
426 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
427 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
429 TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
432 * Open the class id Key
434 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
435 keyName,
436 &clsidKey);
438 if (hres != ERROR_SUCCESS)
439 return REGDB_E_CLASSNOTREG;
442 * Retrieve the size of the name string.
444 cbData = 0;
446 hres = RegQueryValueExA(clsidKey,
448 NULL,
449 &dwKeyType,
450 NULL,
451 &cbData);
453 if (hres!=ERROR_SUCCESS)
455 RegCloseKey(clsidKey);
456 return REGDB_E_READREGDB;
460 * Allocate a buffer for the registry value.
462 *pszUserType = CoTaskMemAlloc(cbData*2);
464 if (*pszUserType==NULL)
466 RegCloseKey(clsidKey);
467 return E_OUTOFMEMORY;
470 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
472 if (buffer == NULL)
474 RegCloseKey(clsidKey);
475 CoTaskMemFree(*pszUserType);
476 *pszUserType=NULL;
477 return E_OUTOFMEMORY;
480 hres = RegQueryValueExA(clsidKey,
482 NULL,
483 &dwKeyType,
484 buffer,
485 &cbData);
487 RegCloseKey(clsidKey);
490 if (hres!=ERROR_SUCCESS)
492 CoTaskMemFree(*pszUserType);
493 *pszUserType=NULL;
495 retVal = REGDB_E_READREGDB;
497 else
499 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
500 retVal = S_OK;
502 HeapFree(GetProcessHeap(), 0, buffer);
504 return retVal;
507 /***********************************************************************
508 * DoDragDrop [OLE32.65]
510 HRESULT WINAPI DoDragDrop (
511 IDataObject *pDataObject, /* [in] ptr to the data obj */
512 IDropSource* pDropSource, /* [in] ptr to the source obj */
513 DWORD dwOKEffect, /* [in] effects allowed by the source */
514 DWORD *pdwEffect) /* [out] ptr to effects of the source */
516 TrackerWindowInfo trackerInfo;
517 HWND hwndTrackWindow;
518 MSG msg;
520 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
523 * Setup the drag n drop tracking window.
525 trackerInfo.dataObject = pDataObject;
526 trackerInfo.dropSource = pDropSource;
527 trackerInfo.dwOKEffect = dwOKEffect;
528 trackerInfo.pdwEffect = pdwEffect;
529 trackerInfo.trackingDone = FALSE;
530 trackerInfo.escPressed = FALSE;
531 trackerInfo.curDragTargetHWND = 0;
532 trackerInfo.curDragTarget = 0;
534 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
535 "TrackerWindow",
536 WS_POPUP,
537 CW_USEDEFAULT, CW_USEDEFAULT,
538 CW_USEDEFAULT, CW_USEDEFAULT,
542 (LPVOID)&trackerInfo);
544 if (hwndTrackWindow!=0)
547 * Capture the mouse input
549 SetCapture(hwndTrackWindow);
552 * Pump messages. All mouse input should go the the capture window.
554 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
556 if ( (msg.message >= WM_KEYFIRST) &&
557 (msg.message <= WM_KEYLAST) )
560 * When keyboard messages are sent to windows on this thread, we
561 * want to ignore notify the drop source that the state changed.
562 * in the case of the Escape key, we also notify the drop source
563 * we give it a special meaning.
565 if ( (msg.message==WM_KEYDOWN) &&
566 (msg.wParam==VK_ESCAPE) )
568 trackerInfo.escPressed = TRUE;
572 * Notify the drop source.
574 OLEDD_TrackStateChange(&trackerInfo,
575 msg.pt,
576 OLEDD_GetButtonState());
578 else
581 * Dispatch the messages only when it's not a keyboard message.
583 DispatchMessageA(&msg);
588 * Destroy the temporary window.
590 DestroyWindow(hwndTrackWindow);
592 return trackerInfo.returnValue;
595 return E_FAIL;
598 /***********************************************************************
599 * OleQueryLinkFromData [OLE32.118]
601 HRESULT WINAPI OleQueryLinkFromData(
602 IDataObject* pSrcDataObject)
604 FIXME("(%p),stub!\n", pSrcDataObject);
605 return S_OK;
608 /***********************************************************************
609 * OleRegGetMiscStatus [OLE32.121]
611 HRESULT WINAPI OleRegGetMiscStatus(
612 REFCLSID clsid,
613 DWORD dwAspect,
614 DWORD* pdwStatus)
616 char keyName[60];
617 HKEY clsidKey;
618 HKEY miscStatusKey;
619 HKEY aspectKey;
620 LONG result;
623 * Initialize the out parameter.
625 *pdwStatus = 0;
628 * Build the key name we're looking for
630 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
631 clsid->Data1, clsid->Data2, clsid->Data3,
632 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
633 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
635 TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
638 * Open the class id Key
640 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
641 keyName,
642 &clsidKey);
644 if (result != ERROR_SUCCESS)
645 return REGDB_E_CLASSNOTREG;
648 * Get the MiscStatus
650 result = RegOpenKeyA(clsidKey,
651 "MiscStatus",
652 &miscStatusKey);
655 if (result != ERROR_SUCCESS)
657 RegCloseKey(clsidKey);
658 return REGDB_E_READREGDB;
662 * Read the default value
664 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
667 * Open the key specific to the requested aspect.
669 sprintf(keyName, "%ld", dwAspect);
671 result = RegOpenKeyA(miscStatusKey,
672 keyName,
673 &aspectKey);
675 if (result == ERROR_SUCCESS)
677 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
678 RegCloseKey(aspectKey);
682 * Cleanup
684 RegCloseKey(miscStatusKey);
685 RegCloseKey(clsidKey);
687 return S_OK;
690 /******************************************************************************
691 * OleSetContainedObject [OLE32.128]
693 HRESULT WINAPI OleSetContainedObject(
694 LPUNKNOWN pUnknown,
695 BOOL fContained)
697 IRunnableObject* runnable = NULL;
698 HRESULT hres;
700 TRACE("(%p,%x), stub!\n", pUnknown, fContained);
702 hres = IUnknown_QueryInterface(pUnknown,
703 &IID_IRunnableObject,
704 (void**)&runnable);
706 if (SUCCEEDED(hres))
708 hres = IRunnableObject_SetContainedObject(runnable, fContained);
710 IRunnableObject_Release(runnable);
712 return hres;
715 return S_OK;
718 /******************************************************************************
719 * OleLoad [OLE32.112]
721 HRESULT WINAPI OleLoad(
722 LPSTORAGE pStg,
723 REFIID riid,
724 LPOLECLIENTSITE pClientSite,
725 LPVOID* ppvObj)
727 IPersistStorage* persistStorage = NULL;
728 IOleObject* oleObject = NULL;
729 STATSTG storageInfo;
730 HRESULT hres;
732 TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
735 * TODO, Conversion ... OleDoAutoConvert
739 * Get the class ID for the object.
741 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
744 * Now, try and create the handler for the object
746 hres = CoCreateInstance(&storageInfo.clsid,
747 NULL,
748 CLSCTX_INPROC_HANDLER,
749 &IID_IOleObject,
750 (void**)&oleObject);
753 * If that fails, as it will most times, load the default
754 * OLE handler.
756 if (FAILED(hres))
758 hres = OleCreateDefaultHandler(&storageInfo.clsid,
759 NULL,
760 &IID_IOleObject,
761 (void**)&oleObject);
765 * If we couldn't find a handler... this is bad. Abort the whole thing.
767 if (FAILED(hres))
768 return hres;
771 * Inform the new object of it's client site.
773 hres = IOleObject_SetClientSite(oleObject, pClientSite);
776 * Initialize the object with it's IPersistStorage interface.
778 hres = IOleObject_QueryInterface(oleObject,
779 &IID_IPersistStorage,
780 (void**)&persistStorage);
782 if (SUCCEEDED(hres))
784 IPersistStorage_Load(persistStorage, pStg);
786 IPersistStorage_Release(persistStorage);
787 persistStorage = NULL;
791 * Return the requested interface to the caller.
793 hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
796 * Cleanup interfaces used internally
798 IOleObject_Release(oleObject);
800 return hres;
803 /***********************************************************************
804 * OleSave [OLE32.124]
806 HRESULT WINAPI OleSave(
807 LPPERSISTSTORAGE pPS,
808 LPSTORAGE pStg,
809 BOOL fSameAsLoad)
811 HRESULT hres;
812 CLSID objectClass;
814 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
817 * First, we transfer the class ID (if available)
819 hres = IPersistStorage_GetClassID(pPS, &objectClass);
821 if (SUCCEEDED(hres))
823 WriteClassStg(pStg, &objectClass);
827 * Then, we ask the object to save itself to the
828 * storage. If it is successful, we commit the storage.
830 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
832 if (SUCCEEDED(hres))
834 IStorage_Commit(pStg,
835 STGC_DEFAULT);
838 return hres;
842 /******************************************************************************
843 * OleLockRunning [OLE32.114]
845 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
847 IRunnableObject* runnable = NULL;
848 HRESULT hres;
850 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
852 hres = IUnknown_QueryInterface(pUnknown,
853 &IID_IRunnableObject,
854 (void**)&runnable);
856 if (SUCCEEDED(hres))
858 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
860 IRunnableObject_Release(runnable);
862 return hres;
864 else
865 return E_INVALIDARG;
869 /**************************************************************************
870 * Internal methods to manage the shared OLE menu in response to the
871 * OLE***MenuDescriptor API
874 /***
875 * OLEMenu_Initialize()
877 * Initializes the OLEMENU data structures.
879 static void OLEMenu_Initialize()
883 /***
884 * OLEMenu_UnInitialize()
886 * Releases the OLEMENU data structures.
888 static void OLEMenu_UnInitialize()
892 /*************************************************************************
893 * OLEMenu_InstallHooks
894 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
896 * RETURNS: TRUE if message hooks were succesfully installed
897 * FALSE on failure
899 BOOL OLEMenu_InstallHooks( DWORD tid )
901 OleMenuHookItem *pHookItem = NULL;
903 /* Create an entry for the hook table */
904 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
905 sizeof(OleMenuHookItem)) ) )
906 return FALSE;
908 pHookItem->tid = tid;
909 pHookItem->hHeap = GetProcessHeap();
911 /* Install a thread scope message hook for WH_GETMESSAGE */
912 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
913 0, GetCurrentThreadId() );
914 if ( !pHookItem->GetMsg_hHook )
915 goto CLEANUP;
917 /* Install a thread scope message hook for WH_CALLWNDPROC */
918 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
919 0, GetCurrentThreadId() );
920 if ( !pHookItem->CallWndProc_hHook )
921 goto CLEANUP;
923 /* Insert the hook table entry */
924 pHookItem->next = hook_list;
925 hook_list = pHookItem;
927 return TRUE;
929 CLEANUP:
930 /* Unhook any hooks */
931 if ( pHookItem->GetMsg_hHook )
932 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
933 if ( pHookItem->CallWndProc_hHook )
934 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
935 /* Release the hook table entry */
936 HeapFree(pHookItem->hHeap, 0, pHookItem );
938 return FALSE;
941 /*************************************************************************
942 * OLEMenu_UnInstallHooks
943 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
945 * RETURNS: TRUE if message hooks were succesfully installed
946 * FALSE on failure
948 BOOL OLEMenu_UnInstallHooks( DWORD tid )
950 OleMenuHookItem *pHookItem = NULL;
951 OleMenuHookItem **ppHook = &hook_list;
953 while (*ppHook)
955 if ((*ppHook)->tid == tid)
957 pHookItem = *ppHook;
958 *ppHook = pHookItem->next;
959 break;
961 ppHook = &(*ppHook)->next;
963 if (!pHookItem) return FALSE;
965 /* Uninstall the hooks installed for this thread */
966 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
967 goto CLEANUP;
968 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
969 goto CLEANUP;
971 /* Release the hook table entry */
972 HeapFree(pHookItem->hHeap, 0, pHookItem );
974 return TRUE;
976 CLEANUP:
977 /* Release the hook table entry */
978 if (pHookItem)
979 HeapFree(pHookItem->hHeap, 0, pHookItem );
981 return FALSE;
984 /*************************************************************************
985 * OLEMenu_IsHookInstalled
986 * Tests if OLEMenu hooks have been installed for a thread
988 * RETURNS: The pointer and index of the hook table entry for the tid
989 * NULL and -1 for the index if no hooks were installed for this thread
991 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
993 OleMenuHookItem *pHookItem = NULL;
995 /* Do a simple linear search for an entry whose tid matches ours.
996 * We really need a map but efficiency is not a concern here. */
997 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
999 if ( tid == pHookItem->tid )
1000 return pHookItem;
1003 return NULL;
1006 /***********************************************************************
1007 * OLEMenu_FindMainMenuIndex
1009 * Used by OLEMenu API to find the top level group a menu item belongs to.
1010 * On success pnPos contains the index of the item in the top level menu group
1012 * RETURNS: TRUE if the ID was found, FALSE on failure
1014 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1016 UINT i, nItems;
1018 nItems = GetMenuItemCount( hMainMenu );
1020 for (i = 0; i < nItems; i++)
1022 HMENU hsubmenu;
1024 /* Is the current item a submenu? */
1025 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1027 /* If the handle is the same we're done */
1028 if ( hsubmenu == hPopupMenu )
1030 if (pnPos)
1031 *pnPos = i;
1032 return TRUE;
1034 /* Recursively search without updating pnPos */
1035 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1037 if (pnPos)
1038 *pnPos = i;
1039 return TRUE;
1044 return FALSE;
1047 /***********************************************************************
1048 * OLEMenu_SetIsServerMenu
1050 * Checks whether a popup menu belongs to a shared menu group which is
1051 * owned by the server, and sets the menu descriptor state accordingly.
1052 * All menu messages from these groups should be routed to the server.
1054 * RETURNS: TRUE if the popup menu is part of a server owned group
1055 * FASE if the popup menu is part of a container owned group
1057 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1059 UINT nPos = 0, nWidth, i;
1061 pOleMenuDescriptor->bIsServerItem = FALSE;
1063 /* Don't bother searching if the popup is the combined menu itself */
1064 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1065 return FALSE;
1067 /* Find the menu item index in the shared OLE menu that this item belongs to */
1068 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1069 return FALSE;
1071 /* The group widths array has counts for the number of elements
1072 * in the groups File, Edit, Container, Object, Window, Help.
1073 * The Edit, Object & Help groups belong to the server object
1074 * and the other three belong to the container.
1075 * Loop through the group widths and locate the group we are a member of.
1077 for ( i = 0, nWidth = 0; i < 6; i++ )
1079 nWidth += pOleMenuDescriptor->mgw.width[i];
1080 if ( nPos < nWidth )
1082 /* Odd elements are server menu widths */
1083 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1084 break;
1088 return pOleMenuDescriptor->bIsServerItem;
1091 /*************************************************************************
1092 * OLEMenu_CallWndProc
1093 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1094 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1096 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1098 LPCWPSTRUCT pMsg = NULL;
1099 HOLEMENU hOleMenu = 0;
1100 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1101 OleMenuHookItem *pHookItem = NULL;
1102 WORD fuFlags;
1104 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1106 /* Check if we're being asked to process the message */
1107 if ( HC_ACTION != code )
1108 goto NEXTHOOK;
1110 /* Retrieve the current message being dispatched from lParam */
1111 pMsg = (LPCWPSTRUCT)lParam;
1113 /* Check if the message is destined for a window we are interested in:
1114 * If the window has an OLEMenu property we may need to dispatch
1115 * the menu message to its active objects window instead. */
1117 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1118 if ( !hOleMenu )
1119 goto NEXTHOOK;
1121 /* Get the menu descriptor */
1122 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1123 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1124 goto NEXTHOOK;
1126 /* Process menu messages */
1127 switch( pMsg->message )
1129 case WM_INITMENU:
1131 /* Reset the menu descriptor state */
1132 pOleMenuDescriptor->bIsServerItem = FALSE;
1134 /* Send this message to the server as well */
1135 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1136 pMsg->message, pMsg->wParam, pMsg->lParam );
1137 goto NEXTHOOK;
1140 case WM_INITMENUPOPUP:
1142 /* Save the state for whether this is a server owned menu */
1143 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1144 break;
1147 case WM_MENUSELECT:
1149 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1150 if ( fuFlags & MF_SYSMENU )
1151 goto NEXTHOOK;
1153 /* Save the state for whether this is a server owned popup menu */
1154 else if ( fuFlags & MF_POPUP )
1155 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1157 break;
1160 case WM_DRAWITEM:
1162 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1163 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1164 goto NEXTHOOK; /* Not a menu message */
1166 break;
1169 default:
1170 goto NEXTHOOK;
1173 /* If the message was for the server dispatch it accordingly */
1174 if ( pOleMenuDescriptor->bIsServerItem )
1176 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1177 pMsg->message, pMsg->wParam, pMsg->lParam );
1180 NEXTHOOK:
1181 if ( pOleMenuDescriptor )
1182 GlobalUnlock( hOleMenu );
1184 /* Lookup the hook item for the current thread */
1185 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1187 /* This should never fail!! */
1188 WARN("could not retrieve hHook for current thread!\n" );
1189 return 0;
1192 /* Pass on the message to the next hooker */
1193 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1196 /*************************************************************************
1197 * OLEMenu_GetMsgProc
1198 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1199 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1201 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1203 LPMSG pMsg = NULL;
1204 HOLEMENU hOleMenu = 0;
1205 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1206 OleMenuHookItem *pHookItem = NULL;
1207 WORD wCode;
1209 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1211 /* Check if we're being asked to process a messages */
1212 if ( HC_ACTION != code )
1213 goto NEXTHOOK;
1215 /* Retrieve the current message being dispatched from lParam */
1216 pMsg = (LPMSG)lParam;
1218 /* Check if the message is destined for a window we are interested in:
1219 * If the window has an OLEMenu property we may need to dispatch
1220 * the menu message to its active objects window instead. */
1222 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1223 if ( !hOleMenu )
1224 goto NEXTHOOK;
1226 /* Process menu messages */
1227 switch( pMsg->message )
1229 case WM_COMMAND:
1231 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1232 if ( wCode )
1233 goto NEXTHOOK; /* Not a menu message */
1234 break;
1236 default:
1237 goto NEXTHOOK;
1240 /* Get the menu descriptor */
1241 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1242 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1243 goto NEXTHOOK;
1245 /* If the message was for the server dispatch it accordingly */
1246 if ( pOleMenuDescriptor->bIsServerItem )
1248 /* Change the hWnd in the message to the active objects hWnd.
1249 * The message loop which reads this message will automatically
1250 * dispatch it to the embedded objects window. */
1251 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1254 NEXTHOOK:
1255 if ( pOleMenuDescriptor )
1256 GlobalUnlock( hOleMenu );
1258 /* Lookup the hook item for the current thread */
1259 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1261 /* This should never fail!! */
1262 WARN("could not retrieve hHook for current thread!\n" );
1263 return FALSE;
1266 /* Pass on the message to the next hooker */
1267 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1270 /***********************************************************************
1271 * OleCreateMenuDescriptor [OLE32.97]
1272 * Creates an OLE menu descriptor for OLE to use when dispatching
1273 * menu messages and commands.
1275 * PARAMS:
1276 * hmenuCombined - Handle to the objects combined menu
1277 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1280 HOLEMENU WINAPI OleCreateMenuDescriptor(
1281 HMENU hmenuCombined,
1282 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1284 HOLEMENU hOleMenu;
1285 OleMenuDescriptor *pOleMenuDescriptor;
1286 int i;
1288 if ( !hmenuCombined || !lpMenuWidths )
1289 return 0;
1291 /* Create an OLE menu descriptor */
1292 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1293 sizeof(OleMenuDescriptor) ) ) )
1294 return 0;
1296 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1297 if ( !pOleMenuDescriptor )
1298 return 0;
1300 /* Initialize menu group widths and hmenu */
1301 for ( i = 0; i < 6; i++ )
1302 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1304 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1305 pOleMenuDescriptor->bIsServerItem = FALSE;
1306 GlobalUnlock( hOleMenu );
1308 return hOleMenu;
1311 /***********************************************************************
1312 * OleDestroyMenuDescriptor [OLE32.99]
1313 * Destroy the shared menu descriptor
1315 HRESULT WINAPI OleDestroyMenuDescriptor(
1316 HOLEMENU hmenuDescriptor)
1318 if ( hmenuDescriptor )
1319 GlobalFree( hmenuDescriptor );
1320 return S_OK;
1323 /***********************************************************************
1324 * OleSetMenuDescriptor [OLE32.129]
1325 * Installs or removes OLE dispatching code for the containers frame window
1326 * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1327 * OLE should install context sensitive help F1 filtering for the app when
1328 * these are non null.
1330 * PARAMS:
1331 * hOleMenu Handle to composite menu descriptor
1332 * hwndFrame Handle to containers frame window
1333 * hwndActiveObject Handle to objects in-place activation window
1334 * lpFrame Pointer to IOleInPlaceFrame on containers window
1335 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1337 * RETURNS:
1338 * S_OK - menu installed correctly
1339 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1341 HRESULT WINAPI OleSetMenuDescriptor(
1342 HOLEMENU hOleMenu,
1343 HWND hwndFrame,
1344 HWND hwndActiveObject,
1345 LPOLEINPLACEFRAME lpFrame,
1346 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1348 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1350 /* Check args */
1351 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1352 return E_INVALIDARG;
1354 if ( lpFrame || lpActiveObject )
1356 FIXME("(%x, %x, %x, %p, %p), Context sensitive help filtering not implemented!\n",
1357 (unsigned int)hOleMenu,
1358 hwndFrame,
1359 hwndActiveObject,
1360 lpFrame,
1361 lpActiveObject);
1364 /* Set up a message hook to intercept the containers frame window messages.
1365 * The message filter is responsible for dispatching menu messages from the
1366 * shared menu which are intended for the object.
1369 if ( hOleMenu ) /* Want to install dispatching code */
1371 /* If OLEMenu hooks are already installed for this thread, fail
1372 * Note: This effectively means that OleSetMenuDescriptor cannot
1373 * be called twice in succession on the same frame window
1374 * without first calling it with a null hOleMenu to uninstall */
1375 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1376 return E_FAIL;
1378 /* Get the menu descriptor */
1379 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1380 if ( !pOleMenuDescriptor )
1381 return E_UNEXPECTED;
1383 /* Update the menu descriptor */
1384 pOleMenuDescriptor->hwndFrame = hwndFrame;
1385 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1387 GlobalUnlock( hOleMenu );
1388 pOleMenuDescriptor = NULL;
1390 /* Add a menu descriptor windows property to the frame window */
1391 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1393 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1394 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1395 return E_FAIL;
1397 else /* Want to uninstall dispatching code */
1399 /* Uninstall the hooks */
1400 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1401 return E_FAIL;
1403 /* Remove the menu descriptor property from the frame window */
1404 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1407 return S_OK;
1410 /******************************************************************************
1411 * IsAccelerator [OLE32.75]
1412 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1414 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1416 /* YES, Accel16! */
1417 LPACCEL16 lpAccelTbl;
1418 int i;
1420 if(!lpMsg) return FALSE;
1421 if (!hAccel || !(lpAccelTbl = (LPACCEL16)LockResource16(hAccel)))
1423 WARN_(accel)("invalid accel handle=%04x\n", hAccel);
1424 return FALSE;
1426 if((lpMsg->message != WM_KEYDOWN &&
1427 lpMsg->message != WM_KEYUP &&
1428 lpMsg->message != WM_SYSKEYDOWN &&
1429 lpMsg->message != WM_SYSKEYUP &&
1430 lpMsg->message != WM_CHAR)) return FALSE;
1432 TRACE_(accel)("hAccel=%04x, cAccelEntries=%d,"
1433 "msg->hwnd=%04x, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1434 hAccel, cAccelEntries,
1435 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1436 for(i = 0; i < cAccelEntries; i++)
1438 if(lpAccelTbl[i].key != lpMsg->wParam)
1439 continue;
1441 if(lpMsg->message == WM_CHAR)
1443 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1445 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1446 goto found;
1449 else
1451 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1453 INT mask = 0;
1454 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1455 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1456 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1457 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1458 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1459 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1460 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1462 else
1464 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1466 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1467 { /* ^^ ALT pressed */
1468 TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1469 goto found;
1476 WARN_(accel)("couldn't translate accelerator key\n");
1477 return FALSE;
1479 found:
1480 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1481 return TRUE;
1484 /***********************************************************************
1485 * ReleaseStgMedium [OLE32.140]
1487 void WINAPI ReleaseStgMedium(
1488 STGMEDIUM* pmedium)
1490 switch (pmedium->tymed)
1492 case TYMED_HGLOBAL:
1494 if ( (pmedium->pUnkForRelease==0) &&
1495 (pmedium->u.hGlobal!=0) )
1496 GlobalFree(pmedium->u.hGlobal);
1498 pmedium->u.hGlobal = 0;
1499 break;
1501 case TYMED_FILE:
1503 if (pmedium->u.lpszFileName!=0)
1505 if (pmedium->pUnkForRelease==0)
1507 DeleteFileW(pmedium->u.lpszFileName);
1510 CoTaskMemFree(pmedium->u.lpszFileName);
1513 pmedium->u.lpszFileName = 0;
1514 break;
1516 case TYMED_ISTREAM:
1518 if (pmedium->u.pstm!=0)
1520 IStream_Release(pmedium->u.pstm);
1523 pmedium->u.pstm = 0;
1524 break;
1526 case TYMED_ISTORAGE:
1528 if (pmedium->u.pstg!=0)
1530 IStorage_Release(pmedium->u.pstg);
1533 pmedium->u.pstg = 0;
1534 break;
1536 case TYMED_GDI:
1538 if ( (pmedium->pUnkForRelease==0) &&
1539 (pmedium->u.hGlobal!=0) )
1540 DeleteObject(pmedium->u.hGlobal);
1542 pmedium->u.hGlobal = 0;
1543 break;
1545 case TYMED_MFPICT:
1547 if ( (pmedium->pUnkForRelease==0) &&
1548 (pmedium->u.hMetaFilePict!=0) )
1550 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hGlobal);
1551 DeleteMetaFile(pMP->hMF);
1552 GlobalUnlock(pmedium->u.hGlobal);
1553 GlobalFree(pmedium->u.hGlobal);
1556 pmedium->u.hMetaFilePict = 0;
1557 break;
1559 case TYMED_ENHMF:
1561 if ( (pmedium->pUnkForRelease==0) &&
1562 (pmedium->u.hEnhMetaFile!=0) )
1564 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1567 pmedium->u.hEnhMetaFile = 0;
1568 break;
1570 case TYMED_NULL:
1571 default:
1572 break;
1576 * After cleaning up, the unknown is released
1578 if (pmedium->pUnkForRelease!=0)
1580 IUnknown_Release(pmedium->pUnkForRelease);
1581 pmedium->pUnkForRelease = 0;
1585 /***
1586 * OLEDD_Initialize()
1588 * Initializes the OLE drag and drop data structures.
1590 static void OLEDD_Initialize()
1592 WNDCLASSA wndClass;
1594 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1595 wndClass.style = CS_GLOBALCLASS;
1596 wndClass.lpfnWndProc = (WNDPROC)OLEDD_DragTrackerWindowProc;
1597 wndClass.cbClsExtra = 0;
1598 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1599 wndClass.hCursor = 0;
1600 wndClass.hbrBackground = 0;
1601 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1603 RegisterClassA (&wndClass);
1606 /***
1607 * OLEDD_UnInitialize()
1609 * Releases the OLE drag and drop data structures.
1611 static void OLEDD_UnInitialize()
1614 * Simply empty the list.
1616 while (targetListHead!=NULL)
1618 RevokeDragDrop(targetListHead->hwndTarget);
1622 /***
1623 * OLEDD_InsertDropTarget()
1625 * Insert the target node in the tree.
1627 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1629 DropTargetNode* curNode;
1630 DropTargetNode** parentNodeLink;
1633 * Iterate the tree to find the insertion point.
1635 curNode = targetListHead;
1636 parentNodeLink = &targetListHead;
1638 while (curNode!=NULL)
1640 if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1643 * If the node we want to add has a smaller HWND, go left
1645 parentNodeLink = &curNode->prevDropTarget;
1646 curNode = curNode->prevDropTarget;
1648 else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1651 * If the node we want to add has a larger HWND, go right
1653 parentNodeLink = &curNode->nextDropTarget;
1654 curNode = curNode->nextDropTarget;
1656 else
1659 * The item was found in the list. It shouldn't have been there
1661 assert(FALSE);
1662 return;
1667 * If we get here, we have found a spot for our item. The parentNodeLink
1668 * pointer points to the pointer that we have to modify.
1669 * The curNode should be NULL. We just have to establish the link and Voila!
1671 assert(curNode==NULL);
1672 assert(parentNodeLink!=NULL);
1673 assert(*parentNodeLink==NULL);
1675 *parentNodeLink=nodeToAdd;
1678 /***
1679 * OLEDD_ExtractDropTarget()
1681 * Removes the target node from the tree.
1683 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1685 DropTargetNode* curNode;
1686 DropTargetNode** parentNodeLink;
1689 * Iterate the tree to find the insertion point.
1691 curNode = targetListHead;
1692 parentNodeLink = &targetListHead;
1694 while (curNode!=NULL)
1696 if (hwndOfTarget<curNode->hwndTarget)
1699 * If the node we want to add has a smaller HWND, go left
1701 parentNodeLink = &curNode->prevDropTarget;
1702 curNode = curNode->prevDropTarget;
1704 else if (hwndOfTarget>curNode->hwndTarget)
1707 * If the node we want to add has a larger HWND, go right
1709 parentNodeLink = &curNode->nextDropTarget;
1710 curNode = curNode->nextDropTarget;
1712 else
1715 * The item was found in the list. Detach it from it's parent and
1716 * re-insert it's kids in the tree.
1718 assert(parentNodeLink!=NULL);
1719 assert(*parentNodeLink==curNode);
1722 * We arbitrately re-attach the left sub-tree to the parent.
1724 *parentNodeLink = curNode->prevDropTarget;
1727 * And we re-insert the right subtree
1729 if (curNode->nextDropTarget!=NULL)
1731 OLEDD_InsertDropTarget(curNode->nextDropTarget);
1735 * The node we found is still a valid node once we complete
1736 * the unlinking of the kids.
1738 curNode->nextDropTarget=NULL;
1739 curNode->prevDropTarget=NULL;
1741 return curNode;
1746 * If we get here, the node is not in the tree
1748 return NULL;
1751 /***
1752 * OLEDD_FindDropTarget()
1754 * Finds information about the drop target.
1756 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1758 DropTargetNode* curNode;
1761 * Iterate the tree to find the HWND value.
1763 curNode = targetListHead;
1765 while (curNode!=NULL)
1767 if (hwndOfTarget<curNode->hwndTarget)
1770 * If the node we want to add has a smaller HWND, go left
1772 curNode = curNode->prevDropTarget;
1774 else if (hwndOfTarget>curNode->hwndTarget)
1777 * If the node we want to add has a larger HWND, go right
1779 curNode = curNode->nextDropTarget;
1781 else
1784 * The item was found in the list.
1786 return curNode;
1791 * If we get here, the item is not in the list
1793 return NULL;
1796 /***
1797 * OLEDD_DragTrackerWindowProc()
1799 * This method is the WindowProcedure of the drag n drop tracking
1800 * window. During a drag n Drop operation, an invisible window is created
1801 * to receive the user input and act upon it. This procedure is in charge
1802 * of this behavior.
1804 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1805 HWND hwnd,
1806 UINT uMsg,
1807 WPARAM wParam,
1808 LPARAM lParam)
1810 switch (uMsg)
1812 case WM_CREATE:
1814 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1816 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1819 break;
1821 case WM_MOUSEMOVE:
1823 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1824 POINT mousePos;
1827 * Get the current mouse position in screen coordinates.
1829 mousePos.x = LOWORD(lParam);
1830 mousePos.y = HIWORD(lParam);
1831 ClientToScreen(hwnd, &mousePos);
1834 * Track the movement of the mouse.
1836 OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1838 break;
1840 case WM_LBUTTONUP:
1841 case WM_MBUTTONUP:
1842 case WM_RBUTTONUP:
1843 case WM_LBUTTONDOWN:
1844 case WM_MBUTTONDOWN:
1845 case WM_RBUTTONDOWN:
1847 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1848 POINT mousePos;
1851 * Get the current mouse position in screen coordinates.
1853 mousePos.x = LOWORD(lParam);
1854 mousePos.y = HIWORD(lParam);
1855 ClientToScreen(hwnd, &mousePos);
1858 * Notify everyone that the button state changed
1859 * TODO: Check if the "escape" key was pressed.
1861 OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1863 break;
1868 * This is a window proc after all. Let's call the default.
1870 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1873 /***
1874 * OLEDD_TrackMouseMove()
1876 * This method is invoked while a drag and drop operation is in effect.
1877 * it will generate the appropriate callbacks in the drop source
1878 * and drop target. It will also provide the expected feedback to
1879 * the user.
1881 * params:
1882 * trackerInfo - Pointer to the structure identifying the
1883 * drag & drop operation that is currently
1884 * active.
1885 * mousePos - Current position of the mouse in screen
1886 * coordinates.
1887 * keyState - Contains the state of the shift keys and the
1888 * mouse buttons (MK_LBUTTON and the like)
1890 static void OLEDD_TrackMouseMove(
1891 TrackerWindowInfo* trackerInfo,
1892 POINT mousePos,
1893 DWORD keyState)
1895 HWND hwndNewTarget = 0;
1896 HRESULT hr = S_OK;
1899 * Get the handle of the window under the mouse
1901 hwndNewTarget = WindowFromPoint(mousePos);
1904 * Every time, we re-initialize the effects passed to the
1905 * IDropTarget to the effects allowed by the source.
1907 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1910 * If we are hovering over the same target as before, send the
1911 * DragOver notification
1913 if ( (trackerInfo->curDragTarget != 0) &&
1914 (trackerInfo->curDragTargetHWND==hwndNewTarget) )
1916 POINTL mousePosParam;
1919 * The documentation tells me that the coordinate should be in the target
1920 * window's coordinate space. However, the tests I made tell me the
1921 * coordinates should be in screen coordinates.
1923 mousePosParam.x = mousePos.x;
1924 mousePosParam.y = mousePos.y;
1926 IDropTarget_DragOver(trackerInfo->curDragTarget,
1927 keyState,
1928 mousePosParam,
1929 trackerInfo->pdwEffect);
1931 else
1933 DropTargetNode* newDropTargetNode = 0;
1936 * If we changed window, we have to notify our old target and check for
1937 * the new one.
1939 if (trackerInfo->curDragTarget!=0)
1941 IDropTarget_DragLeave(trackerInfo->curDragTarget);
1945 * Make sure we're hovering over a window.
1947 if (hwndNewTarget!=0)
1950 * Find-out if there is a drag target under the mouse
1952 HWND nexttar = hwndNewTarget;
1953 do {
1954 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
1955 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
1956 if(nexttar) hwndNewTarget = nexttar;
1958 trackerInfo->curDragTargetHWND = hwndNewTarget;
1959 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1962 * If there is, notify it that we just dragged-in
1964 if (trackerInfo->curDragTarget!=0)
1966 POINTL mousePosParam;
1969 * The documentation tells me that the coordinate should be in the target
1970 * window's coordinate space. However, the tests I made tell me the
1971 * coordinates should be in screen coordinates.
1973 mousePosParam.x = mousePos.x;
1974 mousePosParam.y = mousePos.y;
1976 IDropTarget_DragEnter(trackerInfo->curDragTarget,
1977 trackerInfo->dataObject,
1978 keyState,
1979 mousePosParam,
1980 trackerInfo->pdwEffect);
1983 else
1986 * The mouse is not over a window so we don't track anything.
1988 trackerInfo->curDragTargetHWND = 0;
1989 trackerInfo->curDragTarget = 0;
1994 * Now that we have done that, we have to tell the source to give
1995 * us feedback on the work being done by the target. If we don't
1996 * have a target, simulate no effect.
1998 if (trackerInfo->curDragTarget==0)
2000 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2003 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2004 *trackerInfo->pdwEffect);
2007 * When we ask for feedback from the drop source, sometimes it will
2008 * do all the necessary work and sometimes it will not handle it
2009 * when that's the case, we must display the standard drag and drop
2010 * cursors.
2012 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2014 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2016 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2018 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2020 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2022 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2024 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2026 else
2028 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2033 /***
2034 * OLEDD_TrackStateChange()
2036 * This method is invoked while a drag and drop operation is in effect.
2037 * It is used to notify the drop target/drop source callbacks when
2038 * the state of the keyboard or mouse button change.
2040 * params:
2041 * trackerInfo - Pointer to the structure identifying the
2042 * drag & drop operation that is currently
2043 * active.
2044 * mousePos - Current position of the mouse in screen
2045 * coordinates.
2046 * keyState - Contains the state of the shift keys and the
2047 * mouse buttons (MK_LBUTTON and the like)
2049 static void OLEDD_TrackStateChange(
2050 TrackerWindowInfo* trackerInfo,
2051 POINT mousePos,
2052 DWORD keyState)
2055 * Ask the drop source what to do with the operation.
2057 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2058 trackerInfo->dropSource,
2059 trackerInfo->escPressed,
2060 keyState);
2063 * All the return valued will stop the operation except the S_OK
2064 * return value.
2066 if (trackerInfo->returnValue!=S_OK)
2069 * Make sure the message loop in DoDragDrop stops
2071 trackerInfo->trackingDone = TRUE;
2074 * Release the mouse in case the drop target decides to show a popup
2075 * or a menu or something.
2077 ReleaseCapture();
2080 * If we end-up over a target, drop the object in the target or
2081 * inform the target that the operation was cancelled.
2083 if (trackerInfo->curDragTarget!=0)
2085 switch (trackerInfo->returnValue)
2088 * If the source wants us to complete the operation, we tell
2089 * the drop target that we just dropped the object in it.
2091 case DRAGDROP_S_DROP:
2093 POINTL mousePosParam;
2096 * The documentation tells me that the coordinate should be
2097 * in the target window's coordinate space. However, the tests
2098 * I made tell me the coordinates should be in screen coordinates.
2100 mousePosParam.x = mousePos.x;
2101 mousePosParam.y = mousePos.y;
2103 IDropTarget_Drop(trackerInfo->curDragTarget,
2104 trackerInfo->dataObject,
2105 keyState,
2106 mousePosParam,
2107 trackerInfo->pdwEffect);
2108 break;
2111 * If the source told us that we should cancel, fool the drop
2112 * target by telling it that the mouse left it's window.
2113 * Also set the drop effect to "NONE" in case the application
2114 * ignores the result of DoDragDrop.
2116 case DRAGDROP_S_CANCEL:
2117 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2118 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2119 break;
2125 /***
2126 * OLEDD_GetButtonState()
2128 * This method will use the current state of the keyboard to build
2129 * a button state mask equivalent to the one passed in the
2130 * WM_MOUSEMOVE wParam.
2132 static DWORD OLEDD_GetButtonState()
2134 BYTE keyboardState[256];
2135 DWORD keyMask = 0;
2137 GetKeyboardState(keyboardState);
2139 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2140 keyMask |= MK_SHIFT;
2142 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2143 keyMask |= MK_CONTROL;
2145 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2146 keyMask |= MK_LBUTTON;
2148 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2149 keyMask |= MK_RBUTTON;
2151 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2152 keyMask |= MK_MBUTTON;
2154 return keyMask;
2157 /***
2158 * OLEDD_GetButtonState()
2160 * This method will read the default value of the registry key in
2161 * parameter and extract a DWORD value from it. The registry key value
2162 * can be in a string key or a DWORD key.
2164 * params:
2165 * regKey - Key to read the default value from
2166 * pdwValue - Pointer to the location where the DWORD
2167 * value is returned. This value is not modified
2168 * if the value is not found.
2171 static void OLEUTL_ReadRegistryDWORDValue(
2172 HKEY regKey,
2173 DWORD* pdwValue)
2175 char buffer[20];
2176 DWORD dwKeyType;
2177 DWORD cbData = 20;
2178 LONG lres;
2180 lres = RegQueryValueExA(regKey,
2182 NULL,
2183 &dwKeyType,
2184 (LPBYTE)buffer,
2185 &cbData);
2187 if (lres==ERROR_SUCCESS)
2189 switch (dwKeyType)
2191 case REG_DWORD:
2192 *pdwValue = *(DWORD*)buffer;
2193 break;
2194 case REG_EXPAND_SZ:
2195 case REG_MULTI_SZ:
2196 case REG_SZ:
2197 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2198 break;
2203 /******************************************************************************
2204 * OleMetaFilePictFromIconAndLabel (OLE2.56)
2206 * Returns a global memory handle to a metafile which contains the icon and
2207 * label given.
2208 * I guess the result of that should look somehow like desktop icons.
2209 * If no hIcon is given, we load the icon via lpszSourceFile and iIconIndex.
2210 * This code might be wrong at some places.
2212 HGLOBAL16 WINAPI OleMetaFilePictFromIconAndLabel16(
2213 HICON16 hIcon,
2214 LPCOLESTR16 lpszLabel,
2215 LPCOLESTR16 lpszSourceFile,
2216 UINT16 iIconIndex
2218 METAFILEPICT16 *mf;
2219 HGLOBAL16 hmf;
2220 HDC16 hdc;
2222 FIXME("(%04x, '%s', '%s', %d): incorrect metrics, please try to correct them !\n\n\n", hIcon, lpszLabel, lpszSourceFile, iIconIndex);
2224 if (!hIcon) {
2225 if (lpszSourceFile) {
2226 HINSTANCE16 hInstance = LoadLibrary16(lpszSourceFile);
2228 /* load the icon at index from lpszSourceFile */
2229 hIcon = (HICON16)LoadIconA(hInstance, (LPCSTR)(DWORD)iIconIndex);
2230 FreeLibrary16(hInstance);
2231 } else
2232 return (HGLOBAL)NULL;
2235 hdc = CreateMetaFile16(NULL);
2236 DrawIcon(hdc, 0, 0, hIcon); /* FIXME */
2237 TextOutA(hdc, 0, 0, lpszLabel, 1); /* FIXME */
2238 hmf = GlobalAlloc16(0, sizeof(METAFILEPICT16));
2239 mf = (METAFILEPICT16 *)GlobalLock16(hmf);
2240 mf->mm = MM_ANISOTROPIC;
2241 mf->xExt = 20; /* FIXME: bogus */
2242 mf->yExt = 20; /* dito */
2243 mf->hMF = CloseMetaFile16(hdc);
2244 return hmf;
2247 /******************************************************************************
2248 * DllDebugObjectRPCHook (OLE32.62)
2249 * turns on and off internal debugging, pointer is only used on macintosh
2252 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2254 FIXME("stub\n");
2255 return TRUE;