Fixed some issues found by winapi_check.
[wine/dcerpc.git] / dlls / ole32 / ole2.c
bloba4f3aa622533c6ea21b1278ed20a2262e1c8c28b
2 /*
3 * OLE2 library
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Noel Borthwick
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "commctrl.h"
32 #include "ole2.h"
33 #include "ole2ver.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winuser.h"
38 #include "winreg.h"
40 #include "wine/obj_clientserver.h"
41 #include "wine/winbase16.h"
42 #include "wine/wingdi16.h"
43 #include "wine/winuser16.h"
44 #include "ole32_main.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49 WINE_DECLARE_DEBUG_CHANNEL(accel);
51 /******************************************************************************
52 * These are static/global variables and internal data structures that the
53 * OLE module uses to maintain it's state.
55 typedef struct tagDropTargetNode
57 HWND hwndTarget;
58 IDropTarget* dropTarget;
59 struct tagDropTargetNode* prevDropTarget;
60 struct tagDropTargetNode* nextDropTarget;
61 } DropTargetNode;
63 typedef struct tagTrackerWindowInfo
65 IDataObject* dataObject;
66 IDropSource* dropSource;
67 DWORD dwOKEffect;
68 DWORD* pdwEffect;
69 BOOL trackingDone;
70 HRESULT returnValue;
72 BOOL escPressed;
73 HWND curDragTargetHWND;
74 IDropTarget* curDragTarget;
75 } TrackerWindowInfo;
77 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
79 HWND hwndFrame; /* The containers frame window */
80 HWND hwndActiveObject; /* The active objects window */
81 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
82 HMENU hmenuCombined; /* The combined menu */
83 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
84 } OleMenuDescriptor;
86 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
88 DWORD tid; /* Thread Id */
89 HANDLE hHeap; /* Heap this is allocated from */
90 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
91 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
92 struct tagOleMenuHookItem *next;
93 } OleMenuHookItem;
95 static OleMenuHookItem *hook_list;
98 * This is the lock count on the OLE library. It is controlled by the
99 * OLEInitialize/OLEUninitialize methods.
101 static ULONG OLE_moduleLockCount = 0;
104 * Name of our registered window class.
106 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
109 * This is the head of the Drop target container.
111 static DropTargetNode* targetListHead = NULL;
113 /******************************************************************************
114 * These are the prototypes of miscelaneous utility methods
116 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
118 /******************************************************************************
119 * These are the prototypes of the utility methods used to manage a shared menu
121 static void OLEMenu_Initialize();
122 static void OLEMenu_UnInitialize();
123 BOOL OLEMenu_InstallHooks( DWORD tid );
124 BOOL OLEMenu_UnInstallHooks( DWORD tid );
125 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
126 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
127 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
128 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
129 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
131 /******************************************************************************
132 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
134 void OLEClipbrd_UnInitialize();
135 void OLEClipbrd_Initialize();
137 /******************************************************************************
138 * These are the prototypes of the utility methods used for OLE Drag n Drop
140 static void OLEDD_Initialize();
141 static void OLEDD_UnInitialize();
142 static void OLEDD_InsertDropTarget(
143 DropTargetNode* nodeToAdd);
144 static DropTargetNode* OLEDD_ExtractDropTarget(
145 HWND hwndOfTarget);
146 static DropTargetNode* OLEDD_FindDropTarget(
147 HWND hwndOfTarget);
148 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
149 HWND hwnd,
150 UINT uMsg,
151 WPARAM wParam,
152 LPARAM lParam);
153 static void OLEDD_TrackMouseMove(
154 TrackerWindowInfo* trackerInfo,
155 POINT mousePos,
156 DWORD keyState);
157 static void OLEDD_TrackStateChange(
158 TrackerWindowInfo* trackerInfo,
159 POINT mousePos,
160 DWORD keyState);
161 static DWORD OLEDD_GetButtonState();
164 /******************************************************************************
165 * OleBuildVersion [OLE2.1]
166 * OleBuildVersion [OLE32.84]
168 DWORD WINAPI OleBuildVersion(void)
170 TRACE("Returning version %d, build %d.\n", rmm, rup);
171 return (rmm<<16)+rup;
174 /***********************************************************************
175 * OleInitialize (OLE2.2)
176 * OleInitialize (OLE32.108)
178 HRESULT WINAPI OleInitialize(LPVOID reserved)
180 HRESULT hr;
182 TRACE("(%p)\n", reserved);
185 * The first duty of the OleInitialize is to initialize the COM libraries.
187 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
190 * If the CoInitializeEx call failed, the OLE libraries can't be
191 * initialized.
193 if (FAILED(hr))
194 return hr;
197 * Then, it has to initialize the OLE specific modules.
198 * This includes:
199 * Clipboard
200 * Drag and Drop
201 * Object linking and Embedding
202 * In-place activation
204 if (OLE_moduleLockCount==0)
207 * Initialize the libraries.
209 TRACE("() - Initializing the OLE libraries\n");
212 * OLE Clipboard
214 OLEClipbrd_Initialize();
217 * Drag and Drop
219 OLEDD_Initialize();
222 * OLE shared menu
224 OLEMenu_Initialize();
228 * Then, we increase the lock count on the OLE module.
230 OLE_moduleLockCount++;
232 return hr;
235 /******************************************************************************
236 * CoGetCurrentProcess [COMPOBJ.34]
237 * CoGetCurrentProcess [OLE32.18]
239 * NOTES
240 * Is DWORD really the correct return type for this function?
242 DWORD WINAPI CoGetCurrentProcess(void)
244 return GetCurrentProcessId();
247 /******************************************************************************
248 * OleUninitialize [OLE2.3]
249 * OleUninitialize [OLE32.131]
251 void WINAPI OleUninitialize(void)
253 TRACE("()\n");
256 * Decrease the lock count on the OLE module.
258 OLE_moduleLockCount--;
261 * If we hit the bottom of the lock stack, free the libraries.
263 if (OLE_moduleLockCount==0)
266 * Actually free the libraries.
268 TRACE("() - Freeing the last reference count\n");
271 * OLE Clipboard
273 OLEClipbrd_UnInitialize();
276 * Drag and Drop
278 OLEDD_UnInitialize();
281 * OLE shared menu
283 OLEMenu_UnInitialize();
287 * Then, uninitialize the COM libraries.
289 CoUninitialize();
292 /******************************************************************************
293 * CoRegisterMessageFilter [OLE32.38]
295 HRESULT WINAPI CoRegisterMessageFilter(
296 LPMESSAGEFILTER lpMessageFilter, /* [in] Pointer to interface */
297 LPMESSAGEFILTER *lplpMessageFilter /* [out] Indirect pointer to prior instance if non-NULL */
299 FIXME("stub\n");
300 if (lplpMessageFilter) {
301 *lplpMessageFilter = NULL;
303 return S_OK;
306 /******************************************************************************
307 * OleInitializeWOW [OLE32.109]
309 HRESULT WINAPI OleInitializeWOW(DWORD x) {
310 FIXME("(0x%08lx),stub!\n",x);
311 return 0;
314 /***********************************************************************
315 * RegisterDragDrop (OLE2.35)
317 HRESULT WINAPI RegisterDragDrop16(
318 HWND16 hwnd,
319 LPDROPTARGET pDropTarget
321 FIXME("(0x%04x,%p),stub!\n",hwnd,pDropTarget);
322 return S_OK;
325 /***********************************************************************
326 * RegisterDragDrop (OLE32.139)
328 HRESULT WINAPI RegisterDragDrop(
329 HWND hwnd,
330 LPDROPTARGET pDropTarget)
332 DropTargetNode* dropTargetInfo;
334 TRACE("(0x%x,%p)\n", hwnd, pDropTarget);
337 * First, check if the window is already registered.
339 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
341 if (dropTargetInfo!=NULL)
342 return DRAGDROP_E_ALREADYREGISTERED;
345 * If it's not there, we can add it. We first create a node for it.
347 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
349 if (dropTargetInfo==NULL)
350 return E_OUTOFMEMORY;
352 dropTargetInfo->hwndTarget = hwnd;
353 dropTargetInfo->prevDropTarget = NULL;
354 dropTargetInfo->nextDropTarget = NULL;
357 * Don't forget that this is an interface pointer, need to nail it down since
358 * we keep a copy of it.
360 dropTargetInfo->dropTarget = pDropTarget;
361 IDropTarget_AddRef(dropTargetInfo->dropTarget);
363 OLEDD_InsertDropTarget(dropTargetInfo);
365 return S_OK;
368 /***********************************************************************
369 * RevokeDragDrop (OLE2.36)
371 HRESULT WINAPI RevokeDragDrop16(
372 HWND16 hwnd
374 FIXME("(0x%04x),stub!\n",hwnd);
375 return S_OK;
378 /***********************************************************************
379 * RevokeDragDrop (OLE32.141)
381 HRESULT WINAPI RevokeDragDrop(
382 HWND hwnd)
384 DropTargetNode* dropTargetInfo;
386 TRACE("(0x%x)\n", hwnd);
389 * First, check if the window is already registered.
391 dropTargetInfo = OLEDD_ExtractDropTarget(hwnd);
394 * If it ain't in there, it's an error.
396 if (dropTargetInfo==NULL)
397 return DRAGDROP_E_NOTREGISTERED;
400 * If it's in there, clean-up it's used memory and
401 * references
403 IDropTarget_Release(dropTargetInfo->dropTarget);
404 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
406 return S_OK;
409 /***********************************************************************
410 * OleRegGetUserType (OLE32.122)
412 * This implementation of OleRegGetUserType ignores the dwFormOfType
413 * parameter and always returns the full name of the object. This is
414 * not too bad since this is the case for many objects because of the
415 * way they are registered.
417 HRESULT WINAPI OleRegGetUserType(
418 REFCLSID clsid,
419 DWORD dwFormOfType,
420 LPOLESTR* pszUserType)
422 char keyName[60];
423 DWORD dwKeyType;
424 DWORD cbData;
425 HKEY clsidKey;
426 LONG hres;
427 LPBYTE buffer;
428 HRESULT retVal;
430 * Initialize the out parameter.
432 *pszUserType = NULL;
435 * Build the key name we're looking for
437 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
438 clsid->Data1, clsid->Data2, clsid->Data3,
439 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
440 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
442 TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
445 * Open the class id Key
447 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
448 keyName,
449 &clsidKey);
451 if (hres != ERROR_SUCCESS)
452 return REGDB_E_CLASSNOTREG;
455 * Retrieve the size of the name string.
457 cbData = 0;
459 hres = RegQueryValueExA(clsidKey,
461 NULL,
462 &dwKeyType,
463 NULL,
464 &cbData);
466 if (hres!=ERROR_SUCCESS)
468 RegCloseKey(clsidKey);
469 return REGDB_E_READREGDB;
473 * Allocate a buffer for the registry value.
475 *pszUserType = CoTaskMemAlloc(cbData*2);
477 if (*pszUserType==NULL)
479 RegCloseKey(clsidKey);
480 return E_OUTOFMEMORY;
483 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
485 if (buffer == NULL)
487 RegCloseKey(clsidKey);
488 CoTaskMemFree(*pszUserType);
489 *pszUserType=NULL;
490 return E_OUTOFMEMORY;
493 hres = RegQueryValueExA(clsidKey,
495 NULL,
496 &dwKeyType,
497 buffer,
498 &cbData);
500 RegCloseKey(clsidKey);
503 if (hres!=ERROR_SUCCESS)
505 CoTaskMemFree(*pszUserType);
506 *pszUserType=NULL;
508 retVal = REGDB_E_READREGDB;
510 else
512 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
513 retVal = S_OK;
515 HeapFree(GetProcessHeap(), 0, buffer);
517 return retVal;
520 /***********************************************************************
521 * DoDragDrop [OLE32.65]
523 HRESULT WINAPI DoDragDrop (
524 IDataObject *pDataObject, /* [in] ptr to the data obj */
525 IDropSource* pDropSource, /* [in] ptr to the source obj */
526 DWORD dwOKEffect, /* [in] effects allowed by the source */
527 DWORD *pdwEffect) /* [out] ptr to effects of the source */
529 TrackerWindowInfo trackerInfo;
530 HWND hwndTrackWindow;
531 MSG msg;
533 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
536 * Setup the drag n drop tracking window.
538 trackerInfo.dataObject = pDataObject;
539 trackerInfo.dropSource = pDropSource;
540 trackerInfo.dwOKEffect = dwOKEffect;
541 trackerInfo.pdwEffect = pdwEffect;
542 trackerInfo.trackingDone = FALSE;
543 trackerInfo.escPressed = FALSE;
544 trackerInfo.curDragTargetHWND = 0;
545 trackerInfo.curDragTarget = 0;
547 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
548 "TrackerWindow",
549 WS_POPUP,
550 CW_USEDEFAULT, CW_USEDEFAULT,
551 CW_USEDEFAULT, CW_USEDEFAULT,
555 (LPVOID)&trackerInfo);
557 if (hwndTrackWindow!=0)
560 * Capture the mouse input
562 SetCapture(hwndTrackWindow);
565 * Pump messages. All mouse input should go the the capture window.
567 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
569 if ( (msg.message >= WM_KEYFIRST) &&
570 (msg.message <= WM_KEYLAST) )
573 * When keyboard messages are sent to windows on this thread, we
574 * want to ignore notify the drop source that the state changed.
575 * in the case of the Escape key, we also notify the drop source
576 * we give it a special meaning.
578 if ( (msg.message==WM_KEYDOWN) &&
579 (msg.wParam==VK_ESCAPE) )
581 trackerInfo.escPressed = TRUE;
585 * Notify the drop source.
587 OLEDD_TrackStateChange(&trackerInfo,
588 msg.pt,
589 OLEDD_GetButtonState());
591 else
594 * Dispatch the messages only when it's not a keyboard message.
596 DispatchMessageA(&msg);
601 * Destroy the temporary window.
603 DestroyWindow(hwndTrackWindow);
605 return trackerInfo.returnValue;
608 return E_FAIL;
611 /***********************************************************************
612 * OleQueryLinkFromData [OLE32.118]
614 HRESULT WINAPI OleQueryLinkFromData(
615 IDataObject* pSrcDataObject)
617 FIXME("(%p),stub!\n", pSrcDataObject);
618 return S_OK;
621 /***********************************************************************
622 * OleRegGetMiscStatus [OLE32.121]
624 HRESULT WINAPI OleRegGetMiscStatus(
625 REFCLSID clsid,
626 DWORD dwAspect,
627 DWORD* pdwStatus)
629 char keyName[60];
630 HKEY clsidKey;
631 HKEY miscStatusKey;
632 HKEY aspectKey;
633 LONG result;
636 * Initialize the out parameter.
638 *pdwStatus = 0;
641 * Build the key name we're looking for
643 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
644 clsid->Data1, clsid->Data2, clsid->Data3,
645 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
646 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
648 TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
651 * Open the class id Key
653 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
654 keyName,
655 &clsidKey);
657 if (result != ERROR_SUCCESS)
658 return REGDB_E_CLASSNOTREG;
661 * Get the MiscStatus
663 result = RegOpenKeyA(clsidKey,
664 "MiscStatus",
665 &miscStatusKey);
668 if (result != ERROR_SUCCESS)
670 RegCloseKey(clsidKey);
671 return REGDB_E_READREGDB;
675 * Read the default value
677 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
680 * Open the key specific to the requested aspect.
682 sprintf(keyName, "%ld", dwAspect);
684 result = RegOpenKeyA(miscStatusKey,
685 keyName,
686 &aspectKey);
688 if (result == ERROR_SUCCESS)
690 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
691 RegCloseKey(aspectKey);
695 * Cleanup
697 RegCloseKey(miscStatusKey);
698 RegCloseKey(clsidKey);
700 return S_OK;
703 /******************************************************************************
704 * OleSetContainedObject [OLE32.128]
706 HRESULT WINAPI OleSetContainedObject(
707 LPUNKNOWN pUnknown,
708 BOOL fContained)
710 IRunnableObject* runnable = NULL;
711 HRESULT hres;
713 TRACE("(%p,%x), stub!\n", pUnknown, fContained);
715 hres = IUnknown_QueryInterface(pUnknown,
716 &IID_IRunnableObject,
717 (void**)&runnable);
719 if (SUCCEEDED(hres))
721 hres = IRunnableObject_SetContainedObject(runnable, fContained);
723 IRunnableObject_Release(runnable);
725 return hres;
728 return S_OK;
731 /******************************************************************************
732 * OleLoad [OLE32.112]
734 HRESULT WINAPI OleLoad(
735 LPSTORAGE pStg,
736 REFIID riid,
737 LPOLECLIENTSITE pClientSite,
738 LPVOID* ppvObj)
740 IPersistStorage* persistStorage = NULL;
741 IOleObject* oleObject = NULL;
742 STATSTG storageInfo;
743 HRESULT hres;
745 TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
748 * TODO, Conversion ... OleDoAutoConvert
752 * Get the class ID for the object.
754 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
757 * Now, try and create the handler for the object
759 hres = CoCreateInstance(&storageInfo.clsid,
760 NULL,
761 CLSCTX_INPROC_HANDLER,
762 &IID_IOleObject,
763 (void**)&oleObject);
766 * If that fails, as it will most times, load the default
767 * OLE handler.
769 if (FAILED(hres))
771 hres = OleCreateDefaultHandler(&storageInfo.clsid,
772 NULL,
773 &IID_IOleObject,
774 (void**)&oleObject);
778 * If we couldn't find a handler... this is bad. Abort the whole thing.
780 if (FAILED(hres))
781 return hres;
784 * Inform the new object of it's client site.
786 hres = IOleObject_SetClientSite(oleObject, pClientSite);
789 * Initialize the object with it's IPersistStorage interface.
791 hres = IOleObject_QueryInterface(oleObject,
792 &IID_IPersistStorage,
793 (void**)&persistStorage);
795 if (SUCCEEDED(hres))
797 IPersistStorage_Load(persistStorage, pStg);
799 IPersistStorage_Release(persistStorage);
800 persistStorage = NULL;
804 * Return the requested interface to the caller.
806 hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
809 * Cleanup interfaces used internally
811 IOleObject_Release(oleObject);
813 return hres;
816 /***********************************************************************
817 * OleSave [OLE32.124]
819 HRESULT WINAPI OleSave(
820 LPPERSISTSTORAGE pPS,
821 LPSTORAGE pStg,
822 BOOL fSameAsLoad)
824 HRESULT hres;
825 CLSID objectClass;
827 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
830 * First, we transfer the class ID (if available)
832 hres = IPersistStorage_GetClassID(pPS, &objectClass);
834 if (SUCCEEDED(hres))
836 WriteClassStg(pStg, &objectClass);
840 * Then, we ask the object to save itself to the
841 * storage. If it is successful, we commit the storage.
843 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
845 if (SUCCEEDED(hres))
847 IStorage_Commit(pStg,
848 STGC_DEFAULT);
851 return hres;
855 /******************************************************************************
856 * OleLockRunning [OLE32.114]
858 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
860 IRunnableObject* runnable = NULL;
861 HRESULT hres;
863 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
865 hres = IUnknown_QueryInterface(pUnknown,
866 &IID_IRunnableObject,
867 (void**)&runnable);
869 if (SUCCEEDED(hres))
871 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
873 IRunnableObject_Release(runnable);
875 return hres;
877 else
878 return E_INVALIDARG;
882 /**************************************************************************
883 * Internal methods to manage the shared OLE menu in response to the
884 * OLE***MenuDescriptor API
887 /***
888 * OLEMenu_Initialize()
890 * Initializes the OLEMENU data structures.
892 static void OLEMenu_Initialize()
896 /***
897 * OLEMenu_UnInitialize()
899 * Releases the OLEMENU data structures.
901 static void OLEMenu_UnInitialize()
905 /*************************************************************************
906 * OLEMenu_InstallHooks
907 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
909 * RETURNS: TRUE if message hooks were succesfully installed
910 * FALSE on failure
912 BOOL OLEMenu_InstallHooks( DWORD tid )
914 OleMenuHookItem *pHookItem = NULL;
916 /* Create an entry for the hook table */
917 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
918 sizeof(OleMenuHookItem)) ) )
919 return FALSE;
921 pHookItem->tid = tid;
922 pHookItem->hHeap = GetProcessHeap();
924 /* Install a thread scope message hook for WH_GETMESSAGE */
925 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
926 0, GetCurrentThreadId() );
927 if ( !pHookItem->GetMsg_hHook )
928 goto CLEANUP;
930 /* Install a thread scope message hook for WH_CALLWNDPROC */
931 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
932 0, GetCurrentThreadId() );
933 if ( !pHookItem->CallWndProc_hHook )
934 goto CLEANUP;
936 /* Insert the hook table entry */
937 pHookItem->next = hook_list;
938 hook_list = pHookItem;
940 return TRUE;
942 CLEANUP:
943 /* Unhook any hooks */
944 if ( pHookItem->GetMsg_hHook )
945 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
946 if ( pHookItem->CallWndProc_hHook )
947 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
948 /* Release the hook table entry */
949 HeapFree(pHookItem->hHeap, 0, pHookItem );
951 return FALSE;
954 /*************************************************************************
955 * OLEMenu_UnInstallHooks
956 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
958 * RETURNS: TRUE if message hooks were succesfully installed
959 * FALSE on failure
961 BOOL OLEMenu_UnInstallHooks( DWORD tid )
963 OleMenuHookItem *pHookItem = NULL;
964 OleMenuHookItem **ppHook = &hook_list;
966 while (*ppHook)
968 if ((*ppHook)->tid == tid)
970 pHookItem = *ppHook;
971 *ppHook = pHookItem->next;
972 break;
974 ppHook = &(*ppHook)->next;
976 if (!pHookItem) return FALSE;
978 /* Uninstall the hooks installed for this thread */
979 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
980 goto CLEANUP;
981 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
982 goto CLEANUP;
984 /* Release the hook table entry */
985 HeapFree(pHookItem->hHeap, 0, pHookItem );
987 return TRUE;
989 CLEANUP:
990 /* Release the hook table entry */
991 if (pHookItem)
992 HeapFree(pHookItem->hHeap, 0, pHookItem );
994 return FALSE;
997 /*************************************************************************
998 * OLEMenu_IsHookInstalled
999 * Tests if OLEMenu hooks have been installed for a thread
1001 * RETURNS: The pointer and index of the hook table entry for the tid
1002 * NULL and -1 for the index if no hooks were installed for this thread
1004 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1006 OleMenuHookItem *pHookItem = NULL;
1008 /* Do a simple linear search for an entry whose tid matches ours.
1009 * We really need a map but efficiency is not a concern here. */
1010 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1012 if ( tid == pHookItem->tid )
1013 return pHookItem;
1016 return NULL;
1019 /***********************************************************************
1020 * OLEMenu_FindMainMenuIndex
1022 * Used by OLEMenu API to find the top level group a menu item belongs to.
1023 * On success pnPos contains the index of the item in the top level menu group
1025 * RETURNS: TRUE if the ID was found, FALSE on failure
1027 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1029 UINT i, nItems;
1031 nItems = GetMenuItemCount( hMainMenu );
1033 for (i = 0; i < nItems; i++)
1035 HMENU hsubmenu;
1037 /* Is the current item a submenu? */
1038 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1040 /* If the handle is the same we're done */
1041 if ( hsubmenu == hPopupMenu )
1043 if (pnPos)
1044 *pnPos = i;
1045 return TRUE;
1047 /* Recursively search without updating pnPos */
1048 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1050 if (pnPos)
1051 *pnPos = i;
1052 return TRUE;
1057 return FALSE;
1060 /***********************************************************************
1061 * OLEMenu_SetIsServerMenu
1063 * Checks whether a popup menu belongs to a shared menu group which is
1064 * owned by the server, and sets the menu descriptor state accordingly.
1065 * All menu messages from these groups should be routed to the server.
1067 * RETURNS: TRUE if the popup menu is part of a server owned group
1068 * FASE if the popup menu is part of a container owned group
1070 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1072 UINT nPos = 0, nWidth, i;
1074 pOleMenuDescriptor->bIsServerItem = FALSE;
1076 /* Don't bother searching if the popup is the combined menu itself */
1077 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1078 return FALSE;
1080 /* Find the menu item index in the shared OLE menu that this item belongs to */
1081 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1082 return FALSE;
1084 /* The group widths array has counts for the number of elements
1085 * in the groups File, Edit, Container, Object, Window, Help.
1086 * The Edit, Object & Help groups belong to the server object
1087 * and the other three belong to the container.
1088 * Loop through the group widths and locate the group we are a member of.
1090 for ( i = 0, nWidth = 0; i < 6; i++ )
1092 nWidth += pOleMenuDescriptor->mgw.width[i];
1093 if ( nPos < nWidth )
1095 /* Odd elements are server menu widths */
1096 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1097 break;
1101 return pOleMenuDescriptor->bIsServerItem;
1104 /*************************************************************************
1105 * OLEMenu_CallWndProc
1106 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1107 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1109 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1111 LPCWPSTRUCT pMsg = NULL;
1112 HOLEMENU hOleMenu = 0;
1113 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1114 OleMenuHookItem *pHookItem = NULL;
1115 WORD fuFlags;
1117 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1119 /* Check if we're being asked to process the message */
1120 if ( HC_ACTION != code )
1121 goto NEXTHOOK;
1123 /* Retrieve the current message being dispatched from lParam */
1124 pMsg = (LPCWPSTRUCT)lParam;
1126 /* Check if the message is destined for a window we are interested in:
1127 * If the window has an OLEMenu property we may need to dispatch
1128 * the menu message to its active objects window instead. */
1130 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1131 if ( !hOleMenu )
1132 goto NEXTHOOK;
1134 /* Get the menu descriptor */
1135 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1136 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1137 goto NEXTHOOK;
1139 /* Process menu messages */
1140 switch( pMsg->message )
1142 case WM_INITMENU:
1144 /* Reset the menu descriptor state */
1145 pOleMenuDescriptor->bIsServerItem = FALSE;
1147 /* Send this message to the server as well */
1148 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1149 pMsg->message, pMsg->wParam, pMsg->lParam );
1150 goto NEXTHOOK;
1153 case WM_INITMENUPOPUP:
1155 /* Save the state for whether this is a server owned menu */
1156 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1157 break;
1160 case WM_MENUSELECT:
1162 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1163 if ( fuFlags & MF_SYSMENU )
1164 goto NEXTHOOK;
1166 /* Save the state for whether this is a server owned popup menu */
1167 else if ( fuFlags & MF_POPUP )
1168 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1170 break;
1173 case WM_DRAWITEM:
1175 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1176 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1177 goto NEXTHOOK; /* Not a menu message */
1179 break;
1182 default:
1183 goto NEXTHOOK;
1186 /* If the message was for the server dispatch it accordingly */
1187 if ( pOleMenuDescriptor->bIsServerItem )
1189 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1190 pMsg->message, pMsg->wParam, pMsg->lParam );
1193 NEXTHOOK:
1194 if ( pOleMenuDescriptor )
1195 GlobalUnlock( hOleMenu );
1197 /* Lookup the hook item for the current thread */
1198 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1200 /* This should never fail!! */
1201 WARN("could not retrieve hHook for current thread!\n" );
1202 return 0;
1205 /* Pass on the message to the next hooker */
1206 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1209 /*************************************************************************
1210 * OLEMenu_GetMsgProc
1211 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1212 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1214 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1216 LPMSG pMsg = NULL;
1217 HOLEMENU hOleMenu = 0;
1218 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1219 OleMenuHookItem *pHookItem = NULL;
1220 WORD wCode;
1222 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1224 /* Check if we're being asked to process a messages */
1225 if ( HC_ACTION != code )
1226 goto NEXTHOOK;
1228 /* Retrieve the current message being dispatched from lParam */
1229 pMsg = (LPMSG)lParam;
1231 /* Check if the message is destined for a window we are interested in:
1232 * If the window has an OLEMenu property we may need to dispatch
1233 * the menu message to its active objects window instead. */
1235 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1236 if ( !hOleMenu )
1237 goto NEXTHOOK;
1239 /* Process menu messages */
1240 switch( pMsg->message )
1242 case WM_COMMAND:
1244 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1245 if ( wCode )
1246 goto NEXTHOOK; /* Not a menu message */
1247 break;
1249 default:
1250 goto NEXTHOOK;
1253 /* Get the menu descriptor */
1254 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1255 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1256 goto NEXTHOOK;
1258 /* If the message was for the server dispatch it accordingly */
1259 if ( pOleMenuDescriptor->bIsServerItem )
1261 /* Change the hWnd in the message to the active objects hWnd.
1262 * The message loop which reads this message will automatically
1263 * dispatch it to the embedded objects window. */
1264 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1267 NEXTHOOK:
1268 if ( pOleMenuDescriptor )
1269 GlobalUnlock( hOleMenu );
1271 /* Lookup the hook item for the current thread */
1272 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1274 /* This should never fail!! */
1275 WARN("could not retrieve hHook for current thread!\n" );
1276 return FALSE;
1279 /* Pass on the message to the next hooker */
1280 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1283 /***********************************************************************
1284 * OleCreateMenuDescriptor [OLE32.97]
1285 * Creates an OLE menu descriptor for OLE to use when dispatching
1286 * menu messages and commands.
1288 * PARAMS:
1289 * hmenuCombined - Handle to the objects combined menu
1290 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1293 HOLEMENU WINAPI OleCreateMenuDescriptor(
1294 HMENU hmenuCombined,
1295 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1297 HOLEMENU hOleMenu;
1298 OleMenuDescriptor *pOleMenuDescriptor;
1299 int i;
1301 if ( !hmenuCombined || !lpMenuWidths )
1302 return 0;
1304 /* Create an OLE menu descriptor */
1305 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1306 sizeof(OleMenuDescriptor) ) ) )
1307 return 0;
1309 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1310 if ( !pOleMenuDescriptor )
1311 return 0;
1313 /* Initialize menu group widths and hmenu */
1314 for ( i = 0; i < 6; i++ )
1315 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1317 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1318 pOleMenuDescriptor->bIsServerItem = FALSE;
1319 GlobalUnlock( hOleMenu );
1321 return hOleMenu;
1324 /***********************************************************************
1325 * OleDestroyMenuDescriptor [OLE32.99]
1326 * Destroy the shared menu descriptor
1328 HRESULT WINAPI OleDestroyMenuDescriptor(
1329 HOLEMENU hmenuDescriptor)
1331 if ( hmenuDescriptor )
1332 GlobalFree( hmenuDescriptor );
1333 return S_OK;
1336 /***********************************************************************
1337 * OleSetMenuDescriptor [OLE32.129]
1338 * Installs or removes OLE dispatching code for the containers frame window
1339 * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1340 * OLE should install context sensitive help F1 filtering for the app when
1341 * these are non null.
1343 * PARAMS:
1344 * hOleMenu Handle to composite menu descriptor
1345 * hwndFrame Handle to containers frame window
1346 * hwndActiveObject Handle to objects in-place activation window
1347 * lpFrame Pointer to IOleInPlaceFrame on containers window
1348 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1350 * RETURNS:
1351 * S_OK - menu installed correctly
1352 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1354 HRESULT WINAPI OleSetMenuDescriptor(
1355 HOLEMENU hOleMenu,
1356 HWND hwndFrame,
1357 HWND hwndActiveObject,
1358 LPOLEINPLACEFRAME lpFrame,
1359 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1361 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1363 /* Check args */
1364 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1365 return E_INVALIDARG;
1367 if ( lpFrame || lpActiveObject )
1369 FIXME("(%x, %x, %x, %p, %p), Context sensitive help filtering not implemented!\n",
1370 (unsigned int)hOleMenu,
1371 hwndFrame,
1372 hwndActiveObject,
1373 lpFrame,
1374 lpActiveObject);
1377 /* Set up a message hook to intercept the containers frame window messages.
1378 * The message filter is responsible for dispatching menu messages from the
1379 * shared menu which are intended for the object.
1382 if ( hOleMenu ) /* Want to install dispatching code */
1384 /* If OLEMenu hooks are already installed for this thread, fail
1385 * Note: This effectively means that OleSetMenuDescriptor cannot
1386 * be called twice in succession on the same frame window
1387 * without first calling it with a null hOleMenu to uninstall */
1388 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1389 return E_FAIL;
1391 /* Get the menu descriptor */
1392 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1393 if ( !pOleMenuDescriptor )
1394 return E_UNEXPECTED;
1396 /* Update the menu descriptor */
1397 pOleMenuDescriptor->hwndFrame = hwndFrame;
1398 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1400 GlobalUnlock( hOleMenu );
1401 pOleMenuDescriptor = NULL;
1403 /* Add a menu descriptor windows property to the frame window */
1404 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1406 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1407 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1408 return E_FAIL;
1410 else /* Want to uninstall dispatching code */
1412 /* Uninstall the hooks */
1413 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1414 return E_FAIL;
1416 /* Remove the menu descriptor property from the frame window */
1417 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1420 return S_OK;
1423 /******************************************************************************
1424 * IsAccelerator [OLE32.75]
1425 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1427 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1429 /* YES, Accel16! */
1430 LPACCEL16 lpAccelTbl;
1431 int i;
1433 if(!lpMsg) return FALSE;
1434 if (!hAccel || !(lpAccelTbl = (LPACCEL16)LockResource16(hAccel)))
1436 WARN_(accel)("invalid accel handle=%04x\n", hAccel);
1437 return FALSE;
1439 if((lpMsg->message != WM_KEYDOWN &&
1440 lpMsg->message != WM_KEYUP &&
1441 lpMsg->message != WM_SYSKEYDOWN &&
1442 lpMsg->message != WM_SYSKEYUP &&
1443 lpMsg->message != WM_CHAR)) return FALSE;
1445 TRACE_(accel)("hAccel=%04x, cAccelEntries=%d,"
1446 "msg->hwnd=%04x, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1447 hAccel, cAccelEntries,
1448 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1449 for(i = 0; i < cAccelEntries; i++)
1451 if(lpAccelTbl[i].key != lpMsg->wParam)
1452 continue;
1454 if(lpMsg->message == WM_CHAR)
1456 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1458 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1459 goto found;
1462 else
1464 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1466 INT mask = 0;
1467 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1468 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1469 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1470 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1471 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1472 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1473 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1475 else
1477 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1479 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1480 { /* ^^ ALT pressed */
1481 TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1482 goto found;
1489 WARN_(accel)("couldn't translate accelerator key\n");
1490 return FALSE;
1492 found:
1493 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1494 return TRUE;
1497 /***********************************************************************
1498 * ReleaseStgMedium [OLE32.140]
1500 void WINAPI ReleaseStgMedium(
1501 STGMEDIUM* pmedium)
1503 switch (pmedium->tymed)
1505 case TYMED_HGLOBAL:
1507 if ( (pmedium->pUnkForRelease==0) &&
1508 (pmedium->u.hGlobal!=0) )
1509 GlobalFree(pmedium->u.hGlobal);
1511 pmedium->u.hGlobal = 0;
1512 break;
1514 case TYMED_FILE:
1516 if (pmedium->u.lpszFileName!=0)
1518 if (pmedium->pUnkForRelease==0)
1520 DeleteFileW(pmedium->u.lpszFileName);
1523 CoTaskMemFree(pmedium->u.lpszFileName);
1526 pmedium->u.lpszFileName = 0;
1527 break;
1529 case TYMED_ISTREAM:
1531 if (pmedium->u.pstm!=0)
1533 IStream_Release(pmedium->u.pstm);
1536 pmedium->u.pstm = 0;
1537 break;
1539 case TYMED_ISTORAGE:
1541 if (pmedium->u.pstg!=0)
1543 IStorage_Release(pmedium->u.pstg);
1546 pmedium->u.pstg = 0;
1547 break;
1549 case TYMED_GDI:
1551 if ( (pmedium->pUnkForRelease==0) &&
1552 (pmedium->u.hGlobal!=0) )
1553 DeleteObject(pmedium->u.hGlobal);
1555 pmedium->u.hGlobal = 0;
1556 break;
1558 case TYMED_MFPICT:
1560 if ( (pmedium->pUnkForRelease==0) &&
1561 (pmedium->u.hMetaFilePict!=0) )
1563 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hGlobal);
1564 DeleteMetaFile(pMP->hMF);
1565 GlobalUnlock(pmedium->u.hGlobal);
1566 GlobalFree(pmedium->u.hGlobal);
1569 pmedium->u.hMetaFilePict = 0;
1570 break;
1572 case TYMED_ENHMF:
1574 if ( (pmedium->pUnkForRelease==0) &&
1575 (pmedium->u.hEnhMetaFile!=0) )
1577 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1580 pmedium->u.hEnhMetaFile = 0;
1581 break;
1583 case TYMED_NULL:
1584 default:
1585 break;
1589 * After cleaning up, the unknown is released
1591 if (pmedium->pUnkForRelease!=0)
1593 IUnknown_Release(pmedium->pUnkForRelease);
1594 pmedium->pUnkForRelease = 0;
1598 /***
1599 * OLEDD_Initialize()
1601 * Initializes the OLE drag and drop data structures.
1603 static void OLEDD_Initialize()
1605 WNDCLASSA wndClass;
1607 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1608 wndClass.style = CS_GLOBALCLASS;
1609 wndClass.lpfnWndProc = (WNDPROC)OLEDD_DragTrackerWindowProc;
1610 wndClass.cbClsExtra = 0;
1611 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1612 wndClass.hCursor = 0;
1613 wndClass.hbrBackground = 0;
1614 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1616 RegisterClassA (&wndClass);
1619 /***
1620 * OLEDD_UnInitialize()
1622 * Releases the OLE drag and drop data structures.
1624 static void OLEDD_UnInitialize()
1627 * Simply empty the list.
1629 while (targetListHead!=NULL)
1631 RevokeDragDrop(targetListHead->hwndTarget);
1635 /***
1636 * OLEDD_InsertDropTarget()
1638 * Insert the target node in the tree.
1640 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1642 DropTargetNode* curNode;
1643 DropTargetNode** parentNodeLink;
1646 * Iterate the tree to find the insertion point.
1648 curNode = targetListHead;
1649 parentNodeLink = &targetListHead;
1651 while (curNode!=NULL)
1653 if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1656 * If the node we want to add has a smaller HWND, go left
1658 parentNodeLink = &curNode->prevDropTarget;
1659 curNode = curNode->prevDropTarget;
1661 else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1664 * If the node we want to add has a larger HWND, go right
1666 parentNodeLink = &curNode->nextDropTarget;
1667 curNode = curNode->nextDropTarget;
1669 else
1672 * The item was found in the list. It shouldn't have been there
1674 assert(FALSE);
1675 return;
1680 * If we get here, we have found a spot for our item. The parentNodeLink
1681 * pointer points to the pointer that we have to modify.
1682 * The curNode should be NULL. We just have to establish the link and Voila!
1684 assert(curNode==NULL);
1685 assert(parentNodeLink!=NULL);
1686 assert(*parentNodeLink==NULL);
1688 *parentNodeLink=nodeToAdd;
1691 /***
1692 * OLEDD_ExtractDropTarget()
1694 * Removes the target node from the tree.
1696 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1698 DropTargetNode* curNode;
1699 DropTargetNode** parentNodeLink;
1702 * Iterate the tree to find the insertion point.
1704 curNode = targetListHead;
1705 parentNodeLink = &targetListHead;
1707 while (curNode!=NULL)
1709 if (hwndOfTarget<curNode->hwndTarget)
1712 * If the node we want to add has a smaller HWND, go left
1714 parentNodeLink = &curNode->prevDropTarget;
1715 curNode = curNode->prevDropTarget;
1717 else if (hwndOfTarget>curNode->hwndTarget)
1720 * If the node we want to add has a larger HWND, go right
1722 parentNodeLink = &curNode->nextDropTarget;
1723 curNode = curNode->nextDropTarget;
1725 else
1728 * The item was found in the list. Detach it from it's parent and
1729 * re-insert it's kids in the tree.
1731 assert(parentNodeLink!=NULL);
1732 assert(*parentNodeLink==curNode);
1735 * We arbitrately re-attach the left sub-tree to the parent.
1737 *parentNodeLink = curNode->prevDropTarget;
1740 * And we re-insert the right subtree
1742 if (curNode->nextDropTarget!=NULL)
1744 OLEDD_InsertDropTarget(curNode->nextDropTarget);
1748 * The node we found is still a valid node once we complete
1749 * the unlinking of the kids.
1751 curNode->nextDropTarget=NULL;
1752 curNode->prevDropTarget=NULL;
1754 return curNode;
1759 * If we get here, the node is not in the tree
1761 return NULL;
1764 /***
1765 * OLEDD_FindDropTarget()
1767 * Finds information about the drop target.
1769 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1771 DropTargetNode* curNode;
1774 * Iterate the tree to find the HWND value.
1776 curNode = targetListHead;
1778 while (curNode!=NULL)
1780 if (hwndOfTarget<curNode->hwndTarget)
1783 * If the node we want to add has a smaller HWND, go left
1785 curNode = curNode->prevDropTarget;
1787 else if (hwndOfTarget>curNode->hwndTarget)
1790 * If the node we want to add has a larger HWND, go right
1792 curNode = curNode->nextDropTarget;
1794 else
1797 * The item was found in the list.
1799 return curNode;
1804 * If we get here, the item is not in the list
1806 return NULL;
1809 /***
1810 * OLEDD_DragTrackerWindowProc()
1812 * This method is the WindowProcedure of the drag n drop tracking
1813 * window. During a drag n Drop operation, an invisible window is created
1814 * to receive the user input and act upon it. This procedure is in charge
1815 * of this behavior.
1817 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1818 HWND hwnd,
1819 UINT uMsg,
1820 WPARAM wParam,
1821 LPARAM lParam)
1823 switch (uMsg)
1825 case WM_CREATE:
1827 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1829 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1832 break;
1834 case WM_MOUSEMOVE:
1836 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1837 POINT mousePos;
1840 * Get the current mouse position in screen coordinates.
1842 mousePos.x = LOWORD(lParam);
1843 mousePos.y = HIWORD(lParam);
1844 ClientToScreen(hwnd, &mousePos);
1847 * Track the movement of the mouse.
1849 OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1851 break;
1853 case WM_LBUTTONUP:
1854 case WM_MBUTTONUP:
1855 case WM_RBUTTONUP:
1856 case WM_LBUTTONDOWN:
1857 case WM_MBUTTONDOWN:
1858 case WM_RBUTTONDOWN:
1860 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1861 POINT mousePos;
1864 * Get the current mouse position in screen coordinates.
1866 mousePos.x = LOWORD(lParam);
1867 mousePos.y = HIWORD(lParam);
1868 ClientToScreen(hwnd, &mousePos);
1871 * Notify everyone that the button state changed
1872 * TODO: Check if the "escape" key was pressed.
1874 OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1876 break;
1881 * This is a window proc after all. Let's call the default.
1883 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1886 /***
1887 * OLEDD_TrackMouseMove()
1889 * This method is invoked while a drag and drop operation is in effect.
1890 * it will generate the appropriate callbacks in the drop source
1891 * and drop target. It will also provide the expected feedback to
1892 * the user.
1894 * params:
1895 * trackerInfo - Pointer to the structure identifying the
1896 * drag & drop operation that is currently
1897 * active.
1898 * mousePos - Current position of the mouse in screen
1899 * coordinates.
1900 * keyState - Contains the state of the shift keys and the
1901 * mouse buttons (MK_LBUTTON and the like)
1903 static void OLEDD_TrackMouseMove(
1904 TrackerWindowInfo* trackerInfo,
1905 POINT mousePos,
1906 DWORD keyState)
1908 HWND hwndNewTarget = 0;
1909 HRESULT hr = S_OK;
1912 * Get the handle of the window under the mouse
1914 hwndNewTarget = WindowFromPoint(mousePos);
1917 * Every time, we re-initialize the effects passed to the
1918 * IDropTarget to the effects allowed by the source.
1920 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1923 * If we are hovering over the same target as before, send the
1924 * DragOver notification
1926 if ( (trackerInfo->curDragTarget != 0) &&
1927 (trackerInfo->curDragTargetHWND==hwndNewTarget) )
1929 POINTL mousePosParam;
1932 * The documentation tells me that the coordinate should be in the target
1933 * window's coordinate space. However, the tests I made tell me the
1934 * coordinates should be in screen coordinates.
1936 mousePosParam.x = mousePos.x;
1937 mousePosParam.y = mousePos.y;
1939 IDropTarget_DragOver(trackerInfo->curDragTarget,
1940 keyState,
1941 mousePosParam,
1942 trackerInfo->pdwEffect);
1944 else
1946 DropTargetNode* newDropTargetNode = 0;
1949 * If we changed window, we have to notify our old target and check for
1950 * the new one.
1952 if (trackerInfo->curDragTarget!=0)
1954 IDropTarget_DragLeave(trackerInfo->curDragTarget);
1958 * Make sure we're hovering over a window.
1960 if (hwndNewTarget!=0)
1963 * Find-out if there is a drag target under the mouse
1965 HWND nexttar = hwndNewTarget;
1966 do {
1967 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
1968 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
1969 if(nexttar) hwndNewTarget = nexttar;
1971 trackerInfo->curDragTargetHWND = hwndNewTarget;
1972 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1975 * If there is, notify it that we just dragged-in
1977 if (trackerInfo->curDragTarget!=0)
1979 POINTL mousePosParam;
1982 * The documentation tells me that the coordinate should be in the target
1983 * window's coordinate space. However, the tests I made tell me the
1984 * coordinates should be in screen coordinates.
1986 mousePosParam.x = mousePos.x;
1987 mousePosParam.y = mousePos.y;
1989 IDropTarget_DragEnter(trackerInfo->curDragTarget,
1990 trackerInfo->dataObject,
1991 keyState,
1992 mousePosParam,
1993 trackerInfo->pdwEffect);
1996 else
1999 * The mouse is not over a window so we don't track anything.
2001 trackerInfo->curDragTargetHWND = 0;
2002 trackerInfo->curDragTarget = 0;
2007 * Now that we have done that, we have to tell the source to give
2008 * us feedback on the work being done by the target. If we don't
2009 * have a target, simulate no effect.
2011 if (trackerInfo->curDragTarget==0)
2013 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2016 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2017 *trackerInfo->pdwEffect);
2020 * When we ask for feedback from the drop source, sometimes it will
2021 * do all the necessary work and sometimes it will not handle it
2022 * when that's the case, we must display the standard drag and drop
2023 * cursors.
2025 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2027 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2029 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2031 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2033 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2035 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2037 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2039 else
2041 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2046 /***
2047 * OLEDD_TrackStateChange()
2049 * This method is invoked while a drag and drop operation is in effect.
2050 * It is used to notify the drop target/drop source callbacks when
2051 * the state of the keyboard or mouse button change.
2053 * params:
2054 * trackerInfo - Pointer to the structure identifying the
2055 * drag & drop operation that is currently
2056 * active.
2057 * mousePos - Current position of the mouse in screen
2058 * coordinates.
2059 * keyState - Contains the state of the shift keys and the
2060 * mouse buttons (MK_LBUTTON and the like)
2062 static void OLEDD_TrackStateChange(
2063 TrackerWindowInfo* trackerInfo,
2064 POINT mousePos,
2065 DWORD keyState)
2068 * Ask the drop source what to do with the operation.
2070 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2071 trackerInfo->dropSource,
2072 trackerInfo->escPressed,
2073 keyState);
2076 * All the return valued will stop the operation except the S_OK
2077 * return value.
2079 if (trackerInfo->returnValue!=S_OK)
2082 * Make sure the message loop in DoDragDrop stops
2084 trackerInfo->trackingDone = TRUE;
2087 * Release the mouse in case the drop target decides to show a popup
2088 * or a menu or something.
2090 ReleaseCapture();
2093 * If we end-up over a target, drop the object in the target or
2094 * inform the target that the operation was cancelled.
2096 if (trackerInfo->curDragTarget!=0)
2098 switch (trackerInfo->returnValue)
2101 * If the source wants us to complete the operation, we tell
2102 * the drop target that we just dropped the object in it.
2104 case DRAGDROP_S_DROP:
2106 POINTL mousePosParam;
2109 * The documentation tells me that the coordinate should be
2110 * in the target window's coordinate space. However, the tests
2111 * I made tell me the coordinates should be in screen coordinates.
2113 mousePosParam.x = mousePos.x;
2114 mousePosParam.y = mousePos.y;
2116 IDropTarget_Drop(trackerInfo->curDragTarget,
2117 trackerInfo->dataObject,
2118 keyState,
2119 mousePosParam,
2120 trackerInfo->pdwEffect);
2121 break;
2124 * If the source told us that we should cancel, fool the drop
2125 * target by telling it that the mouse left it's window.
2126 * Also set the drop effect to "NONE" in case the application
2127 * ignores the result of DoDragDrop.
2129 case DRAGDROP_S_CANCEL:
2130 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2131 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2132 break;
2138 /***
2139 * OLEDD_GetButtonState()
2141 * This method will use the current state of the keyboard to build
2142 * a button state mask equivalent to the one passed in the
2143 * WM_MOUSEMOVE wParam.
2145 static DWORD OLEDD_GetButtonState()
2147 BYTE keyboardState[256];
2148 DWORD keyMask = 0;
2150 GetKeyboardState(keyboardState);
2152 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2153 keyMask |= MK_SHIFT;
2155 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2156 keyMask |= MK_CONTROL;
2158 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2159 keyMask |= MK_LBUTTON;
2161 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2162 keyMask |= MK_RBUTTON;
2164 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2165 keyMask |= MK_MBUTTON;
2167 return keyMask;
2170 /***
2171 * OLEDD_GetButtonState()
2173 * This method will read the default value of the registry key in
2174 * parameter and extract a DWORD value from it. The registry key value
2175 * can be in a string key or a DWORD key.
2177 * params:
2178 * regKey - Key to read the default value from
2179 * pdwValue - Pointer to the location where the DWORD
2180 * value is returned. This value is not modified
2181 * if the value is not found.
2184 static void OLEUTL_ReadRegistryDWORDValue(
2185 HKEY regKey,
2186 DWORD* pdwValue)
2188 char buffer[20];
2189 DWORD dwKeyType;
2190 DWORD cbData = 20;
2191 LONG lres;
2193 lres = RegQueryValueExA(regKey,
2195 NULL,
2196 &dwKeyType,
2197 (LPBYTE)buffer,
2198 &cbData);
2200 if (lres==ERROR_SUCCESS)
2202 switch (dwKeyType)
2204 case REG_DWORD:
2205 *pdwValue = *(DWORD*)buffer;
2206 break;
2207 case REG_EXPAND_SZ:
2208 case REG_MULTI_SZ:
2209 case REG_SZ:
2210 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2211 break;
2216 /******************************************************************************
2217 * OleMetaFilePictFromIconAndLabel (OLE2.56)
2219 * Returns a global memory handle to a metafile which contains the icon and
2220 * label given.
2221 * I guess the result of that should look somehow like desktop icons.
2222 * If no hIcon is given, we load the icon via lpszSourceFile and iIconIndex.
2223 * This code might be wrong at some places.
2225 HGLOBAL16 WINAPI OleMetaFilePictFromIconAndLabel16(
2226 HICON16 hIcon,
2227 LPCOLESTR16 lpszLabel,
2228 LPCOLESTR16 lpszSourceFile,
2229 UINT16 iIconIndex
2231 METAFILEPICT16 *mf;
2232 HGLOBAL16 hmf;
2233 HDC16 hdc;
2235 FIXME("(%04x, '%s', '%s', %d): incorrect metrics, please try to correct them !\n\n\n", hIcon, lpszLabel, lpszSourceFile, iIconIndex);
2237 if (!hIcon) {
2238 if (lpszSourceFile) {
2239 HINSTANCE16 hInstance = LoadLibrary16(lpszSourceFile);
2241 /* load the icon at index from lpszSourceFile */
2242 hIcon = (HICON16)LoadIconA(hInstance, (LPCSTR)(DWORD)iIconIndex);
2243 FreeLibrary16(hInstance);
2244 } else
2245 return (HGLOBAL)NULL;
2248 hdc = CreateMetaFile16(NULL);
2249 DrawIcon(hdc, 0, 0, hIcon); /* FIXME */
2250 TextOutA(hdc, 0, 0, lpszLabel, 1); /* FIXME */
2251 hmf = GlobalAlloc16(0, sizeof(METAFILEPICT16));
2252 mf = (METAFILEPICT16 *)GlobalLock16(hmf);
2253 mf->mm = MM_ANISOTROPIC;
2254 mf->xExt = 20; /* FIXME: bogus */
2255 mf->yExt = 20; /* dito */
2256 mf->hMF = CloseMetaFile16(hdc);
2257 return hmf;
2260 /******************************************************************************
2261 * DllDebugObjectRPCHook (OLE32.62)
2262 * turns on and off internal debugging, pointer is only used on macintosh
2265 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2267 FIXME("stub\n");
2268 return TRUE;