4 * Copyright 1995 Martin von Loewis
5 * Copyright 1999 Francis Beaudet
6 * Copyright 1999 Noel Borthwick
7 * Copyright 1999, 2000 Marcus Meissner
8 * Copyright 2005 Juan Lang
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
49 #include "wine/winbase16.h"
50 #include "wine/wingdi16.h"
51 #include "wine/winuser16.h"
52 #include "compobj_private.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(ole);
57 WINE_DECLARE_DEBUG_CHANNEL(accel);
59 #define HICON_16(h32) (LOWORD(h32))
60 #define HICON_32(h16) ((HICON)(ULONG_PTR)(h16))
61 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
63 /******************************************************************************
64 * These are static/global variables and internal data structures that the
65 * OLE module uses to maintain it's state.
67 typedef struct tagDropTargetNode
70 IDropTarget* dropTarget;
71 struct tagDropTargetNode* prevDropTarget;
72 struct tagDropTargetNode* nextDropTarget;
75 typedef struct tagTrackerWindowInfo
77 IDataObject* dataObject;
78 IDropSource* dropSource;
85 HWND curTargetHWND; /* window the mouse is hovering over */
86 HWND curDragTargetHWND; /* might be a ancestor of curTargetHWND */
87 IDropTarget* curDragTarget;
90 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
92 HWND hwndFrame; /* The containers frame window */
93 HWND hwndActiveObject; /* The active objects window */
94 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
95 HMENU hmenuCombined; /* The combined menu */
96 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
99 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
101 DWORD tid; /* Thread Id */
102 HANDLE hHeap; /* Heap this is allocated from */
103 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
104 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
105 struct tagOleMenuHookItem *next;
108 static OleMenuHookItem *hook_list;
111 * This is the lock count on the OLE library. It is controlled by the
112 * OLEInitialize/OLEUninitialize methods.
114 static ULONG OLE_moduleLockCount = 0;
117 * Name of our registered window class.
119 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
122 * This is the head of the Drop target container.
124 static DropTargetNode* targetListHead = NULL;
126 /******************************************************************************
127 * These are the prototypes of miscelaneous utility methods
129 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
131 /******************************************************************************
132 * These are the prototypes of the utility methods used to manage a shared menu
134 static void OLEMenu_Initialize(void);
135 static void OLEMenu_UnInitialize(void);
136 BOOL OLEMenu_InstallHooks( DWORD tid );
137 BOOL OLEMenu_UnInstallHooks( DWORD tid );
138 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
139 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
140 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
141 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
142 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
144 /******************************************************************************
145 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
147 extern void OLEClipbrd_UnInitialize(void);
148 extern void OLEClipbrd_Initialize(void);
150 /******************************************************************************
151 * These are the prototypes of the utility methods used for OLE Drag n Drop
153 static void OLEDD_Initialize(void);
154 static void OLEDD_UnInitialize(void);
155 static void OLEDD_InsertDropTarget(
156 DropTargetNode* nodeToAdd);
157 static DropTargetNode* OLEDD_ExtractDropTarget(
159 static DropTargetNode* OLEDD_FindDropTarget(
161 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
166 static void OLEDD_TrackMouseMove(
167 TrackerWindowInfo* trackerInfo,
170 static void OLEDD_TrackStateChange(
171 TrackerWindowInfo* trackerInfo,
174 static DWORD OLEDD_GetButtonState(void);
177 /******************************************************************************
178 * OleBuildVersion [OLE2.1]
179 * OleBuildVersion [OLE32.@]
181 DWORD WINAPI OleBuildVersion(void)
183 TRACE("Returning version %d, build %d.\n", rmm, rup);
184 return (rmm<<16)+rup;
187 /***********************************************************************
188 * OleInitialize (OLE2.2)
189 * OleInitialize (OLE32.@)
191 HRESULT WINAPI OleInitialize(LPVOID reserved)
195 TRACE("(%p)\n", reserved);
198 * The first duty of the OleInitialize is to initialize the COM libraries.
200 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
203 * If the CoInitializeEx call failed, the OLE libraries can't be
210 * Then, it has to initialize the OLE specific modules.
214 * Object linking and Embedding
215 * In-place activation
217 if (OLE_moduleLockCount==0)
220 * Initialize the libraries.
222 TRACE("() - Initializing the OLE libraries\n");
227 OLEClipbrd_Initialize();
237 OLEMenu_Initialize();
241 * Then, we increase the lock count on the OLE module.
243 OLE_moduleLockCount++;
248 /******************************************************************************
249 * OleUninitialize [OLE2.3]
250 * OleUninitialize [OLE32.@]
252 void WINAPI OleUninitialize(void)
257 * Decrease the lock count on the OLE module.
259 OLE_moduleLockCount--;
262 * If we hit the bottom of the lock stack, free the libraries.
264 if (OLE_moduleLockCount==0)
267 * Actually free the libraries.
269 TRACE("() - Freeing the last reference count\n");
274 OLEClipbrd_UnInitialize();
279 OLEDD_UnInitialize();
284 OLEMenu_UnInitialize();
288 * Then, uninitialize the COM libraries.
293 /******************************************************************************
294 * OleInitializeWOW [OLE32.@]
296 HRESULT WINAPI OleInitializeWOW(DWORD x) {
297 FIXME("(0x%08lx),stub!\n",x);
301 /***********************************************************************
302 * RegisterDragDrop (OLE32.@)
304 HRESULT WINAPI RegisterDragDrop(
306 LPDROPTARGET pDropTarget)
308 DropTargetNode* dropTargetInfo;
310 TRACE("(%p,%p)\n", hwnd, pDropTarget);
316 * First, check if the window is already registered.
318 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
320 if (dropTargetInfo!=NULL)
321 return DRAGDROP_E_ALREADYREGISTERED;
324 * If it's not there, we can add it. We first create a node for it.
326 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
328 if (dropTargetInfo==NULL)
329 return E_OUTOFMEMORY;
331 dropTargetInfo->hwndTarget = hwnd;
332 dropTargetInfo->prevDropTarget = NULL;
333 dropTargetInfo->nextDropTarget = NULL;
336 * Don't forget that this is an interface pointer, need to nail it down since
337 * we keep a copy of it.
339 dropTargetInfo->dropTarget = pDropTarget;
340 IDropTarget_AddRef(dropTargetInfo->dropTarget);
342 OLEDD_InsertDropTarget(dropTargetInfo);
347 /***********************************************************************
348 * RevokeDragDrop (OLE32.@)
350 HRESULT WINAPI RevokeDragDrop(
353 DropTargetNode* dropTargetInfo;
355 TRACE("(%p)\n", hwnd);
358 * First, check if the window is already registered.
360 dropTargetInfo = OLEDD_ExtractDropTarget(hwnd);
363 * If it ain't in there, it's an error.
365 if (dropTargetInfo==NULL)
366 return DRAGDROP_E_NOTREGISTERED;
369 * If it's in there, clean-up it's used memory and
372 IDropTarget_Release(dropTargetInfo->dropTarget);
373 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
378 /***********************************************************************
379 * OleRegGetUserType (OLE32.@)
381 * This implementation of OleRegGetUserType ignores the dwFormOfType
382 * parameter and always returns the full name of the object. This is
383 * not too bad since this is the case for many objects because of the
384 * way they are registered.
386 HRESULT WINAPI OleRegGetUserType(
389 LPOLESTR* pszUserType)
399 * Initialize the out parameter.
404 * Build the key name we're looking for
406 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
407 clsid->Data1, clsid->Data2, clsid->Data3,
408 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
409 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
411 TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
414 * Open the class id Key
416 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
420 if (hres != ERROR_SUCCESS)
421 return REGDB_E_CLASSNOTREG;
424 * Retrieve the size of the name string.
428 hres = RegQueryValueExA(clsidKey,
435 if (hres!=ERROR_SUCCESS)
437 RegCloseKey(clsidKey);
438 return REGDB_E_READREGDB;
442 * Allocate a buffer for the registry value.
444 *pszUserType = CoTaskMemAlloc(cbData*2);
446 if (*pszUserType==NULL)
448 RegCloseKey(clsidKey);
449 return E_OUTOFMEMORY;
452 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
456 RegCloseKey(clsidKey);
457 CoTaskMemFree(*pszUserType);
459 return E_OUTOFMEMORY;
462 hres = RegQueryValueExA(clsidKey,
469 RegCloseKey(clsidKey);
472 if (hres!=ERROR_SUCCESS)
474 CoTaskMemFree(*pszUserType);
477 retVal = REGDB_E_READREGDB;
481 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
484 HeapFree(GetProcessHeap(), 0, buffer);
489 /***********************************************************************
490 * DoDragDrop [OLE32.@]
492 HRESULT WINAPI DoDragDrop (
493 IDataObject *pDataObject, /* [in] ptr to the data obj */
494 IDropSource* pDropSource, /* [in] ptr to the source obj */
495 DWORD dwOKEffect, /* [in] effects allowed by the source */
496 DWORD *pdwEffect) /* [out] ptr to effects of the source */
498 TrackerWindowInfo trackerInfo;
499 HWND hwndTrackWindow;
502 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
505 * Setup the drag n drop tracking window.
507 if (!IsValidInterface((LPUNKNOWN)pDropSource))
510 trackerInfo.dataObject = pDataObject;
511 trackerInfo.dropSource = pDropSource;
512 trackerInfo.dwOKEffect = dwOKEffect;
513 trackerInfo.pdwEffect = pdwEffect;
514 trackerInfo.trackingDone = FALSE;
515 trackerInfo.escPressed = FALSE;
516 trackerInfo.curDragTargetHWND = 0;
517 trackerInfo.curTargetHWND = 0;
518 trackerInfo.curDragTarget = 0;
520 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
523 CW_USEDEFAULT, CW_USEDEFAULT,
524 CW_USEDEFAULT, CW_USEDEFAULT,
528 (LPVOID)&trackerInfo);
530 if (hwndTrackWindow!=0)
533 * Capture the mouse input
535 SetCapture(hwndTrackWindow);
538 * Pump messages. All mouse input should go the the capture window.
540 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
542 if ( (msg.message >= WM_KEYFIRST) &&
543 (msg.message <= WM_KEYLAST) )
546 * When keyboard messages are sent to windows on this thread, we
547 * want to ignore notify the drop source that the state changed.
548 * in the case of the Escape key, we also notify the drop source
549 * we give it a special meaning.
551 if ( (msg.message==WM_KEYDOWN) &&
552 (msg.wParam==VK_ESCAPE) )
554 trackerInfo.escPressed = TRUE;
558 * Notify the drop source.
560 OLEDD_TrackStateChange(&trackerInfo,
562 OLEDD_GetButtonState());
567 * Dispatch the messages only when it's not a keyboard message.
569 DispatchMessageA(&msg);
574 * Destroy the temporary window.
576 DestroyWindow(hwndTrackWindow);
578 return trackerInfo.returnValue;
584 /***********************************************************************
585 * OleQueryLinkFromData [OLE32.@]
587 HRESULT WINAPI OleQueryLinkFromData(
588 IDataObject* pSrcDataObject)
590 FIXME("(%p),stub!\n", pSrcDataObject);
594 /***********************************************************************
595 * OleRegGetMiscStatus [OLE32.@]
597 HRESULT WINAPI OleRegGetMiscStatus(
609 * Initialize the out parameter.
614 * Build the key name we're looking for
616 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
617 clsid->Data1, clsid->Data2, clsid->Data3,
618 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
619 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
621 TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
624 * Open the class id Key
626 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
630 if (result != ERROR_SUCCESS)
631 return REGDB_E_CLASSNOTREG;
636 result = RegOpenKeyA(clsidKey,
641 if (result != ERROR_SUCCESS)
643 RegCloseKey(clsidKey);
644 return REGDB_E_READREGDB;
648 * Read the default value
650 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
653 * Open the key specific to the requested aspect.
655 sprintf(keyName, "%ld", dwAspect);
657 result = RegOpenKeyA(miscStatusKey,
661 if (result == ERROR_SUCCESS)
663 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
664 RegCloseKey(aspectKey);
670 RegCloseKey(miscStatusKey);
671 RegCloseKey(clsidKey);
676 /******************************************************************************
677 * OleSetContainedObject [OLE32.@]
679 HRESULT WINAPI OleSetContainedObject(
683 IRunnableObject* runnable = NULL;
686 TRACE("(%p,%x), stub!\n", pUnknown, fContained);
688 hres = IUnknown_QueryInterface(pUnknown,
689 &IID_IRunnableObject,
694 hres = IRunnableObject_SetContainedObject(runnable, fContained);
696 IRunnableObject_Release(runnable);
704 /******************************************************************************
707 HRESULT WINAPI OleLoad(
710 LPOLECLIENTSITE pClientSite,
713 IPersistStorage* persistStorage = NULL;
714 IOleObject* oleObject = NULL;
718 TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
721 * TODO, Conversion ... OleDoAutoConvert
725 * Get the class ID for the object.
727 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
730 * Now, try and create the handler for the object
732 hres = CoCreateInstance(&storageInfo.clsid,
734 CLSCTX_INPROC_HANDLER,
739 * If that fails, as it will most times, load the default
744 hres = OleCreateDefaultHandler(&storageInfo.clsid,
751 * If we couldn't find a handler... this is bad. Abort the whole thing.
757 * Inform the new object of it's client site.
759 hres = IOleObject_SetClientSite(oleObject, pClientSite);
762 * Initialize the object with it's IPersistStorage interface.
764 hres = IOleObject_QueryInterface(oleObject,
765 &IID_IPersistStorage,
766 (void**)&persistStorage);
770 IPersistStorage_Load(persistStorage, pStg);
772 IPersistStorage_Release(persistStorage);
773 persistStorage = NULL;
777 * Return the requested interface to the caller.
779 hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
782 * Cleanup interfaces used internally
784 IOleObject_Release(oleObject);
789 /***********************************************************************
792 HRESULT WINAPI OleSave(
793 LPPERSISTSTORAGE pPS,
800 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
803 * First, we transfer the class ID (if available)
805 hres = IPersistStorage_GetClassID(pPS, &objectClass);
809 WriteClassStg(pStg, &objectClass);
813 * Then, we ask the object to save itself to the
814 * storage. If it is successful, we commit the storage.
816 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
820 IStorage_Commit(pStg,
828 /******************************************************************************
829 * OleLockRunning [OLE32.@]
831 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
833 IRunnableObject* runnable = NULL;
836 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
838 hres = IUnknown_QueryInterface(pUnknown,
839 &IID_IRunnableObject,
844 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
846 IRunnableObject_Release(runnable);
855 /**************************************************************************
856 * Internal methods to manage the shared OLE menu in response to the
857 * OLE***MenuDescriptor API
861 * OLEMenu_Initialize()
863 * Initializes the OLEMENU data structures.
865 static void OLEMenu_Initialize()
870 * OLEMenu_UnInitialize()
872 * Releases the OLEMENU data structures.
874 static void OLEMenu_UnInitialize()
878 /*************************************************************************
879 * OLEMenu_InstallHooks
880 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
882 * RETURNS: TRUE if message hooks were successfully installed
885 BOOL OLEMenu_InstallHooks( DWORD tid )
887 OleMenuHookItem *pHookItem = NULL;
889 /* Create an entry for the hook table */
890 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
891 sizeof(OleMenuHookItem)) ) )
894 pHookItem->tid = tid;
895 pHookItem->hHeap = GetProcessHeap();
897 /* Install a thread scope message hook for WH_GETMESSAGE */
898 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
899 0, GetCurrentThreadId() );
900 if ( !pHookItem->GetMsg_hHook )
903 /* Install a thread scope message hook for WH_CALLWNDPROC */
904 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
905 0, GetCurrentThreadId() );
906 if ( !pHookItem->CallWndProc_hHook )
909 /* Insert the hook table entry */
910 pHookItem->next = hook_list;
911 hook_list = pHookItem;
916 /* Unhook any hooks */
917 if ( pHookItem->GetMsg_hHook )
918 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
919 if ( pHookItem->CallWndProc_hHook )
920 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
921 /* Release the hook table entry */
922 HeapFree(pHookItem->hHeap, 0, pHookItem );
927 /*************************************************************************
928 * OLEMenu_UnInstallHooks
929 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
931 * RETURNS: TRUE if message hooks were successfully installed
934 BOOL OLEMenu_UnInstallHooks( DWORD tid )
936 OleMenuHookItem *pHookItem = NULL;
937 OleMenuHookItem **ppHook = &hook_list;
941 if ((*ppHook)->tid == tid)
944 *ppHook = pHookItem->next;
947 ppHook = &(*ppHook)->next;
949 if (!pHookItem) return FALSE;
951 /* Uninstall the hooks installed for this thread */
952 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
954 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
957 /* Release the hook table entry */
958 HeapFree(pHookItem->hHeap, 0, pHookItem );
963 /* Release the hook table entry */
964 HeapFree(pHookItem->hHeap, 0, pHookItem );
969 /*************************************************************************
970 * OLEMenu_IsHookInstalled
971 * Tests if OLEMenu hooks have been installed for a thread
973 * RETURNS: The pointer and index of the hook table entry for the tid
974 * NULL and -1 for the index if no hooks were installed for this thread
976 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
978 OleMenuHookItem *pHookItem = NULL;
980 /* Do a simple linear search for an entry whose tid matches ours.
981 * We really need a map but efficiency is not a concern here. */
982 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
984 if ( tid == pHookItem->tid )
991 /***********************************************************************
992 * OLEMenu_FindMainMenuIndex
994 * Used by OLEMenu API to find the top level group a menu item belongs to.
995 * On success pnPos contains the index of the item in the top level menu group
997 * RETURNS: TRUE if the ID was found, FALSE on failure
999 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1003 nItems = GetMenuItemCount( hMainMenu );
1005 for (i = 0; i < nItems; i++)
1009 /* Is the current item a submenu? */
1010 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1012 /* If the handle is the same we're done */
1013 if ( hsubmenu == hPopupMenu )
1019 /* Recursively search without updating pnPos */
1020 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1032 /***********************************************************************
1033 * OLEMenu_SetIsServerMenu
1035 * Checks whether a popup menu belongs to a shared menu group which is
1036 * owned by the server, and sets the menu descriptor state accordingly.
1037 * All menu messages from these groups should be routed to the server.
1039 * RETURNS: TRUE if the popup menu is part of a server owned group
1040 * FALSE if the popup menu is part of a container owned group
1042 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1044 UINT nPos = 0, nWidth, i;
1046 pOleMenuDescriptor->bIsServerItem = FALSE;
1048 /* Don't bother searching if the popup is the combined menu itself */
1049 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1052 /* Find the menu item index in the shared OLE menu that this item belongs to */
1053 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1056 /* The group widths array has counts for the number of elements
1057 * in the groups File, Edit, Container, Object, Window, Help.
1058 * The Edit, Object & Help groups belong to the server object
1059 * and the other three belong to the container.
1060 * Loop through the group widths and locate the group we are a member of.
1062 for ( i = 0, nWidth = 0; i < 6; i++ )
1064 nWidth += pOleMenuDescriptor->mgw.width[i];
1065 if ( nPos < nWidth )
1067 /* Odd elements are server menu widths */
1068 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1073 return pOleMenuDescriptor->bIsServerItem;
1076 /*************************************************************************
1077 * OLEMenu_CallWndProc
1078 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1079 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1081 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1083 LPCWPSTRUCT pMsg = NULL;
1084 HOLEMENU hOleMenu = 0;
1085 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1086 OleMenuHookItem *pHookItem = NULL;
1089 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1091 /* Check if we're being asked to process the message */
1092 if ( HC_ACTION != code )
1095 /* Retrieve the current message being dispatched from lParam */
1096 pMsg = (LPCWPSTRUCT)lParam;
1098 /* Check if the message is destined for a window we are interested in:
1099 * If the window has an OLEMenu property we may need to dispatch
1100 * the menu message to its active objects window instead. */
1102 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1106 /* Get the menu descriptor */
1107 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1108 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1111 /* Process menu messages */
1112 switch( pMsg->message )
1116 /* Reset the menu descriptor state */
1117 pOleMenuDescriptor->bIsServerItem = FALSE;
1119 /* Send this message to the server as well */
1120 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1121 pMsg->message, pMsg->wParam, pMsg->lParam );
1125 case WM_INITMENUPOPUP:
1127 /* Save the state for whether this is a server owned menu */
1128 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1134 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1135 if ( fuFlags & MF_SYSMENU )
1138 /* Save the state for whether this is a server owned popup menu */
1139 else if ( fuFlags & MF_POPUP )
1140 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1147 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1148 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1149 goto NEXTHOOK; /* Not a menu message */
1158 /* If the message was for the server dispatch it accordingly */
1159 if ( pOleMenuDescriptor->bIsServerItem )
1161 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1162 pMsg->message, pMsg->wParam, pMsg->lParam );
1166 if ( pOleMenuDescriptor )
1167 GlobalUnlock( hOleMenu );
1169 /* Lookup the hook item for the current thread */
1170 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1172 /* This should never fail!! */
1173 WARN("could not retrieve hHook for current thread!\n" );
1177 /* Pass on the message to the next hooker */
1178 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1181 /*************************************************************************
1182 * OLEMenu_GetMsgProc
1183 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1184 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1186 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1189 HOLEMENU hOleMenu = 0;
1190 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1191 OleMenuHookItem *pHookItem = NULL;
1194 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1196 /* Check if we're being asked to process a messages */
1197 if ( HC_ACTION != code )
1200 /* Retrieve the current message being dispatched from lParam */
1201 pMsg = (LPMSG)lParam;
1203 /* Check if the message is destined for a window we are interested in:
1204 * If the window has an OLEMenu property we may need to dispatch
1205 * the menu message to its active objects window instead. */
1207 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1211 /* Process menu messages */
1212 switch( pMsg->message )
1216 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1218 goto NEXTHOOK; /* Not a menu message */
1225 /* Get the menu descriptor */
1226 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1227 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1230 /* If the message was for the server dispatch it accordingly */
1231 if ( pOleMenuDescriptor->bIsServerItem )
1233 /* Change the hWnd in the message to the active objects hWnd.
1234 * The message loop which reads this message will automatically
1235 * dispatch it to the embedded objects window. */
1236 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1240 if ( pOleMenuDescriptor )
1241 GlobalUnlock( hOleMenu );
1243 /* Lookup the hook item for the current thread */
1244 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1246 /* This should never fail!! */
1247 WARN("could not retrieve hHook for current thread!\n" );
1251 /* Pass on the message to the next hooker */
1252 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1255 /***********************************************************************
1256 * OleCreateMenuDescriptor [OLE32.@]
1257 * Creates an OLE menu descriptor for OLE to use when dispatching
1258 * menu messages and commands.
1261 * hmenuCombined - Handle to the objects combined menu
1262 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1265 HOLEMENU WINAPI OleCreateMenuDescriptor(
1266 HMENU hmenuCombined,
1267 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1270 OleMenuDescriptor *pOleMenuDescriptor;
1273 if ( !hmenuCombined || !lpMenuWidths )
1276 /* Create an OLE menu descriptor */
1277 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1278 sizeof(OleMenuDescriptor) ) ) )
1281 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1282 if ( !pOleMenuDescriptor )
1285 /* Initialize menu group widths and hmenu */
1286 for ( i = 0; i < 6; i++ )
1287 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1289 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1290 pOleMenuDescriptor->bIsServerItem = FALSE;
1291 GlobalUnlock( hOleMenu );
1296 /***********************************************************************
1297 * OleDestroyMenuDescriptor [OLE32.@]
1298 * Destroy the shared menu descriptor
1300 HRESULT WINAPI OleDestroyMenuDescriptor(
1301 HOLEMENU hmenuDescriptor)
1303 if ( hmenuDescriptor )
1304 GlobalFree( hmenuDescriptor );
1308 /***********************************************************************
1309 * OleSetMenuDescriptor [OLE32.@]
1310 * Installs or removes OLE dispatching code for the containers frame window
1311 * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1312 * OLE should install context sensitive help F1 filtering for the app when
1313 * these are non null.
1316 * hOleMenu Handle to composite menu descriptor
1317 * hwndFrame Handle to containers frame window
1318 * hwndActiveObject Handle to objects in-place activation window
1319 * lpFrame Pointer to IOleInPlaceFrame on containers window
1320 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1323 * S_OK - menu installed correctly
1324 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1326 HRESULT WINAPI OleSetMenuDescriptor(
1329 HWND hwndActiveObject,
1330 LPOLEINPLACEFRAME lpFrame,
1331 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1333 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1336 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1337 return E_INVALIDARG;
1339 if ( lpFrame || lpActiveObject )
1341 FIXME("(%x, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1342 (unsigned int)hOleMenu,
1349 /* Set up a message hook to intercept the containers frame window messages.
1350 * The message filter is responsible for dispatching menu messages from the
1351 * shared menu which are intended for the object.
1354 if ( hOleMenu ) /* Want to install dispatching code */
1356 /* If OLEMenu hooks are already installed for this thread, fail
1357 * Note: This effectively means that OleSetMenuDescriptor cannot
1358 * be called twice in succession on the same frame window
1359 * without first calling it with a null hOleMenu to uninstall */
1360 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1363 /* Get the menu descriptor */
1364 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1365 if ( !pOleMenuDescriptor )
1366 return E_UNEXPECTED;
1368 /* Update the menu descriptor */
1369 pOleMenuDescriptor->hwndFrame = hwndFrame;
1370 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1372 GlobalUnlock( hOleMenu );
1373 pOleMenuDescriptor = NULL;
1375 /* Add a menu descriptor windows property to the frame window */
1376 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1378 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1379 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1382 else /* Want to uninstall dispatching code */
1384 /* Uninstall the hooks */
1385 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1388 /* Remove the menu descriptor property from the frame window */
1389 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1395 /******************************************************************************
1396 * IsAccelerator [OLE32.@]
1397 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1399 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1404 if(!lpMsg) return FALSE;
1407 WARN_(accel)("NULL accel handle\n");
1410 if((lpMsg->message != WM_KEYDOWN &&
1411 lpMsg->message != WM_KEYUP &&
1412 lpMsg->message != WM_SYSKEYDOWN &&
1413 lpMsg->message != WM_SYSKEYUP &&
1414 lpMsg->message != WM_CHAR)) return FALSE;
1415 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1416 if (NULL == lpAccelTbl)
1420 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1422 WARN_(accel)("CopyAcceleratorTableW failed\n");
1423 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1427 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1428 "msg->hwnd=%p, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1429 hAccel, cAccelEntries,
1430 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1431 for(i = 0; i < cAccelEntries; i++)
1433 if(lpAccelTbl[i].key != lpMsg->wParam)
1436 if(lpMsg->message == WM_CHAR)
1438 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1440 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1446 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1449 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1450 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1451 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1452 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1453 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1454 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1455 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1459 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1461 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1462 { /* ^^ ALT pressed */
1463 TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1471 WARN_(accel)("couldn't translate accelerator key\n");
1472 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1476 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1477 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1481 /***********************************************************************
1482 * ReleaseStgMedium [OLE32.@]
1484 void WINAPI ReleaseStgMedium(
1487 switch (pmedium->tymed)
1491 if ( (pmedium->pUnkForRelease==0) &&
1492 (pmedium->u.hGlobal!=0) )
1493 GlobalFree(pmedium->u.hGlobal);
1498 if (pmedium->u.lpszFileName!=0)
1500 if (pmedium->pUnkForRelease==0)
1502 DeleteFileW(pmedium->u.lpszFileName);
1505 CoTaskMemFree(pmedium->u.lpszFileName);
1511 if (pmedium->u.pstm!=0)
1513 IStream_Release(pmedium->u.pstm);
1517 case TYMED_ISTORAGE:
1519 if (pmedium->u.pstg!=0)
1521 IStorage_Release(pmedium->u.pstg);
1527 if ( (pmedium->pUnkForRelease==0) &&
1528 (pmedium->u.hBitmap!=0) )
1529 DeleteObject(pmedium->u.hBitmap);
1534 if ( (pmedium->pUnkForRelease==0) &&
1535 (pmedium->u.hMetaFilePict!=0) )
1537 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1538 DeleteMetaFile(pMP->hMF);
1539 GlobalUnlock(pmedium->u.hMetaFilePict);
1540 GlobalFree(pmedium->u.hMetaFilePict);
1546 if ( (pmedium->pUnkForRelease==0) &&
1547 (pmedium->u.hEnhMetaFile!=0) )
1549 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1557 pmedium->tymed=TYMED_NULL;
1560 * After cleaning up, the unknown is released
1562 if (pmedium->pUnkForRelease!=0)
1564 IUnknown_Release(pmedium->pUnkForRelease);
1565 pmedium->pUnkForRelease = 0;
1570 * OLEDD_Initialize()
1572 * Initializes the OLE drag and drop data structures.
1574 static void OLEDD_Initialize()
1578 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1579 wndClass.style = CS_GLOBALCLASS;
1580 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
1581 wndClass.cbClsExtra = 0;
1582 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1583 wndClass.hCursor = 0;
1584 wndClass.hbrBackground = 0;
1585 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1587 RegisterClassA (&wndClass);
1591 * OLEDD_UnInitialize()
1593 * Releases the OLE drag and drop data structures.
1595 static void OLEDD_UnInitialize()
1598 * Simply empty the list.
1600 while (targetListHead!=NULL)
1602 RevokeDragDrop(targetListHead->hwndTarget);
1607 * OLEDD_InsertDropTarget()
1609 * Insert the target node in the tree.
1611 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1613 DropTargetNode* curNode;
1614 DropTargetNode** parentNodeLink;
1617 * Iterate the tree to find the insertion point.
1619 curNode = targetListHead;
1620 parentNodeLink = &targetListHead;
1622 while (curNode!=NULL)
1624 if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1627 * If the node we want to add has a smaller HWND, go left
1629 parentNodeLink = &curNode->prevDropTarget;
1630 curNode = curNode->prevDropTarget;
1632 else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1635 * If the node we want to add has a larger HWND, go right
1637 parentNodeLink = &curNode->nextDropTarget;
1638 curNode = curNode->nextDropTarget;
1643 * The item was found in the list. It shouldn't have been there
1651 * If we get here, we have found a spot for our item. The parentNodeLink
1652 * pointer points to the pointer that we have to modify.
1653 * The curNode should be NULL. We just have to establish the link and Voila!
1655 assert(curNode==NULL);
1656 assert(parentNodeLink!=NULL);
1657 assert(*parentNodeLink==NULL);
1659 *parentNodeLink=nodeToAdd;
1663 * OLEDD_ExtractDropTarget()
1665 * Removes the target node from the tree.
1667 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1669 DropTargetNode* curNode;
1670 DropTargetNode** parentNodeLink;
1673 * Iterate the tree to find the insertion point.
1675 curNode = targetListHead;
1676 parentNodeLink = &targetListHead;
1678 while (curNode!=NULL)
1680 if (hwndOfTarget<curNode->hwndTarget)
1683 * If the node we want to add has a smaller HWND, go left
1685 parentNodeLink = &curNode->prevDropTarget;
1686 curNode = curNode->prevDropTarget;
1688 else if (hwndOfTarget>curNode->hwndTarget)
1691 * If the node we want to add has a larger HWND, go right
1693 parentNodeLink = &curNode->nextDropTarget;
1694 curNode = curNode->nextDropTarget;
1699 * The item was found in the list. Detach it from it's parent and
1700 * re-insert it's kids in the tree.
1702 assert(parentNodeLink!=NULL);
1703 assert(*parentNodeLink==curNode);
1706 * We arbitrately re-attach the left sub-tree to the parent.
1708 *parentNodeLink = curNode->prevDropTarget;
1711 * And we re-insert the right subtree
1713 if (curNode->nextDropTarget!=NULL)
1715 OLEDD_InsertDropTarget(curNode->nextDropTarget);
1719 * The node we found is still a valid node once we complete
1720 * the unlinking of the kids.
1722 curNode->nextDropTarget=NULL;
1723 curNode->prevDropTarget=NULL;
1730 * If we get here, the node is not in the tree
1736 * OLEDD_FindDropTarget()
1738 * Finds information about the drop target.
1740 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1742 DropTargetNode* curNode;
1745 * Iterate the tree to find the HWND value.
1747 curNode = targetListHead;
1749 while (curNode!=NULL)
1751 if (hwndOfTarget<curNode->hwndTarget)
1754 * If the node we want to add has a smaller HWND, go left
1756 curNode = curNode->prevDropTarget;
1758 else if (hwndOfTarget>curNode->hwndTarget)
1761 * If the node we want to add has a larger HWND, go right
1763 curNode = curNode->nextDropTarget;
1768 * The item was found in the list.
1775 * If we get here, the item is not in the list
1781 * OLEDD_DragTrackerWindowProc()
1783 * This method is the WindowProcedure of the drag n drop tracking
1784 * window. During a drag n Drop operation, an invisible window is created
1785 * to receive the user input and act upon it. This procedure is in charge
1788 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1798 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1800 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1807 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1811 * Get the current mouse position in screen coordinates.
1813 mousePos.x = LOWORD(lParam);
1814 mousePos.y = HIWORD(lParam);
1815 ClientToScreen(hwnd, &mousePos);
1818 * Track the movement of the mouse.
1820 OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1827 case WM_LBUTTONDOWN:
1828 case WM_MBUTTONDOWN:
1829 case WM_RBUTTONDOWN:
1831 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1835 * Get the current mouse position in screen coordinates.
1837 mousePos.x = LOWORD(lParam);
1838 mousePos.y = HIWORD(lParam);
1839 ClientToScreen(hwnd, &mousePos);
1842 * Notify everyone that the button state changed
1843 * TODO: Check if the "escape" key was pressed.
1845 OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1852 * This is a window proc after all. Let's call the default.
1854 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1858 * OLEDD_TrackMouseMove()
1860 * This method is invoked while a drag and drop operation is in effect.
1861 * it will generate the appropriate callbacks in the drop source
1862 * and drop target. It will also provide the expected feedback to
1866 * trackerInfo - Pointer to the structure identifying the
1867 * drag & drop operation that is currently
1869 * mousePos - Current position of the mouse in screen
1871 * keyState - Contains the state of the shift keys and the
1872 * mouse buttons (MK_LBUTTON and the like)
1874 static void OLEDD_TrackMouseMove(
1875 TrackerWindowInfo* trackerInfo,
1879 HWND hwndNewTarget = 0;
1883 * Get the handle of the window under the mouse
1885 hwndNewTarget = WindowFromPoint(mousePos);
1888 * Every time, we re-initialize the effects passed to the
1889 * IDropTarget to the effects allowed by the source.
1891 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1894 * If we are hovering over the same target as before, send the
1895 * DragOver notification
1897 if ( (trackerInfo->curDragTarget != 0) &&
1898 (trackerInfo->curTargetHWND == hwndNewTarget) )
1900 POINTL mousePosParam;
1903 * The documentation tells me that the coordinate should be in the target
1904 * window's coordinate space. However, the tests I made tell me the
1905 * coordinates should be in screen coordinates.
1907 mousePosParam.x = mousePos.x;
1908 mousePosParam.y = mousePos.y;
1910 IDropTarget_DragOver(trackerInfo->curDragTarget,
1913 trackerInfo->pdwEffect);
1917 DropTargetNode* newDropTargetNode = 0;
1920 * If we changed window, we have to notify our old target and check for
1923 if (trackerInfo->curDragTarget!=0)
1925 IDropTarget_DragLeave(trackerInfo->curDragTarget);
1929 * Make sure we're hovering over a window.
1931 if (hwndNewTarget!=0)
1934 * Find-out if there is a drag target under the mouse
1936 HWND nexttar = hwndNewTarget;
1937 trackerInfo->curTargetHWND = hwndNewTarget;
1940 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
1941 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
1942 if(nexttar) hwndNewTarget = nexttar;
1944 trackerInfo->curDragTargetHWND = hwndNewTarget;
1945 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1948 * If there is, notify it that we just dragged-in
1950 if (trackerInfo->curDragTarget!=0)
1952 POINTL mousePosParam;
1955 * The documentation tells me that the coordinate should be in the target
1956 * window's coordinate space. However, the tests I made tell me the
1957 * coordinates should be in screen coordinates.
1959 mousePosParam.x = mousePos.x;
1960 mousePosParam.y = mousePos.y;
1962 IDropTarget_DragEnter(trackerInfo->curDragTarget,
1963 trackerInfo->dataObject,
1966 trackerInfo->pdwEffect);
1972 * The mouse is not over a window so we don't track anything.
1974 trackerInfo->curDragTargetHWND = 0;
1975 trackerInfo->curTargetHWND = 0;
1976 trackerInfo->curDragTarget = 0;
1981 * Now that we have done that, we have to tell the source to give
1982 * us feedback on the work being done by the target. If we don't
1983 * have a target, simulate no effect.
1985 if (trackerInfo->curDragTarget==0)
1987 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
1990 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
1991 *trackerInfo->pdwEffect);
1994 * When we ask for feedback from the drop source, sometimes it will
1995 * do all the necessary work and sometimes it will not handle it
1996 * when that's the case, we must display the standard drag and drop
1999 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2001 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2003 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2005 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2007 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2009 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2011 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2015 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2021 * OLEDD_TrackStateChange()
2023 * This method is invoked while a drag and drop operation is in effect.
2024 * It is used to notify the drop target/drop source callbacks when
2025 * the state of the keyboard or mouse button change.
2028 * trackerInfo - Pointer to the structure identifying the
2029 * drag & drop operation that is currently
2031 * mousePos - Current position of the mouse in screen
2033 * keyState - Contains the state of the shift keys and the
2034 * mouse buttons (MK_LBUTTON and the like)
2036 static void OLEDD_TrackStateChange(
2037 TrackerWindowInfo* trackerInfo,
2042 * Ask the drop source what to do with the operation.
2044 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2045 trackerInfo->dropSource,
2046 trackerInfo->escPressed,
2050 * All the return valued will stop the operation except the S_OK
2053 if (trackerInfo->returnValue!=S_OK)
2056 * Make sure the message loop in DoDragDrop stops
2058 trackerInfo->trackingDone = TRUE;
2061 * Release the mouse in case the drop target decides to show a popup
2062 * or a menu or something.
2067 * If we end-up over a target, drop the object in the target or
2068 * inform the target that the operation was cancelled.
2070 if (trackerInfo->curDragTarget!=0)
2072 switch (trackerInfo->returnValue)
2075 * If the source wants us to complete the operation, we tell
2076 * the drop target that we just dropped the object in it.
2078 case DRAGDROP_S_DROP:
2080 POINTL mousePosParam;
2083 * The documentation tells me that the coordinate should be
2084 * in the target window's coordinate space. However, the tests
2085 * I made tell me the coordinates should be in screen coordinates.
2087 mousePosParam.x = mousePos.x;
2088 mousePosParam.y = mousePos.y;
2090 IDropTarget_Drop(trackerInfo->curDragTarget,
2091 trackerInfo->dataObject,
2094 trackerInfo->pdwEffect);
2098 * If the source told us that we should cancel, fool the drop
2099 * target by telling it that the mouse left it's window.
2100 * Also set the drop effect to "NONE" in case the application
2101 * ignores the result of DoDragDrop.
2103 case DRAGDROP_S_CANCEL:
2104 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2105 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2113 * OLEDD_GetButtonState()
2115 * This method will use the current state of the keyboard to build
2116 * a button state mask equivalent to the one passed in the
2117 * WM_MOUSEMOVE wParam.
2119 static DWORD OLEDD_GetButtonState()
2121 BYTE keyboardState[256];
2124 GetKeyboardState(keyboardState);
2126 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2127 keyMask |= MK_SHIFT;
2129 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2130 keyMask |= MK_CONTROL;
2132 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2133 keyMask |= MK_LBUTTON;
2135 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2136 keyMask |= MK_RBUTTON;
2138 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2139 keyMask |= MK_MBUTTON;
2145 * OLEDD_GetButtonState()
2147 * This method will read the default value of the registry key in
2148 * parameter and extract a DWORD value from it. The registry key value
2149 * can be in a string key or a DWORD key.
2152 * regKey - Key to read the default value from
2153 * pdwValue - Pointer to the location where the DWORD
2154 * value is returned. This value is not modified
2155 * if the value is not found.
2158 static void OLEUTL_ReadRegistryDWORDValue(
2167 lres = RegQueryValueExA(regKey,
2174 if (lres==ERROR_SUCCESS)
2179 *pdwValue = *(DWORD*)buffer;
2184 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2190 /******************************************************************************
2193 * The operation of this function is documented literally in the WinAPI
2194 * documentation to involve a QueryInterface for the IViewObject interface,
2195 * followed by a call to IViewObject::Draw.
2197 HRESULT WINAPI OleDraw(
2204 IViewObject *viewobject;
2206 hres = IUnknown_QueryInterface(pUnk,
2208 (void**)&viewobject);
2210 if (SUCCEEDED(hres))
2214 rectl.left = lprcBounds->left;
2215 rectl.right = lprcBounds->right;
2216 rectl.top = lprcBounds->top;
2217 rectl.bottom = lprcBounds->bottom;
2218 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2220 IViewObject_Release(viewobject);
2225 return DV_E_NOIVIEWOBJECT;
2229 /***********************************************************************
2230 * OleTranslateAccelerator [OLE32.@]
2232 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2233 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2237 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2239 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2240 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2245 /******************************************************************************
2246 * OleCreate [OLE32.@]
2249 HRESULT WINAPI OleCreate(
2253 LPFORMATETC pFormatEtc,
2254 LPOLECLIENTSITE pClientSite,
2258 HRESULT hres, hres1;
2259 IUnknown * pUnk = NULL;
2261 FIXME("\n\t%s\n\t%s semi-stub!\n", debugstr_guid(rclsid), debugstr_guid(riid));
2263 if (SUCCEEDED((hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER|CLSCTX_LOCAL_SERVER , riid, (LPVOID*)&pUnk))))
2268 IPersistStorage * pPS;
2269 if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IOleObject, (LPVOID*)&pOE))))
2271 TRACE("trying to set clientsite %p\n", pClientSite);
2272 hres1 = IOleObject_SetClientSite(pOE, pClientSite);
2273 TRACE("-- result 0x%08lx\n", hres1);
2274 IOleObject_Release(pOE);
2276 if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2278 TRACE("trying to set stg %p\n", pStg);
2279 hres1 = IPersistStorage_InitNew(pPS, pStg);
2280 TRACE("-- result 0x%08lx\n", hres1);
2281 IPersistStorage_Release(pPS);
2288 TRACE("-- %p \n", pUnk);
2292 /******************************************************************************
2293 * OleSetAutoConvert [OLE32.@]
2295 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2298 char buf[200], szClsidNew[200];
2301 /* FIXME: convert to Unicode */
2302 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2303 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2304 WINE_StringFromCLSID(clsidNew, szClsidNew);
2305 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2307 res = REGDB_E_CLASSNOTREG;
2310 if (RegSetValueA(hkey, "AutoConvertTo", REG_SZ, szClsidNew, strlen(szClsidNew)+1))
2312 res = REGDB_E_WRITEREGDB;
2317 if (hkey) RegCloseKey(hkey);
2321 /******************************************************************************
2322 * OleDoAutoConvert [OLE32.@]
2324 HRESULT WINAPI OleDoAutoConvert(IStorage *pStg, LPCLSID pClsidNew)
2326 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2330 /***********************************************************************
2331 * OLE_FreeClipDataArray [internal]
2334 * frees the data associated with an array of CLIPDATAs
2336 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2339 for (i = 0; i < count; i++)
2340 if (pClipDataArray[i].pClipData)
2341 CoTaskMemFree(pClipDataArray[i].pClipData);
2344 /***********************************************************************
2345 * PropSysAllocString [OLE32.@]
2347 * Basically a copy of SysAllocStringLen.
2349 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2353 WCHAR* stringBuffer;
2358 len = lstrlenW(str);
2360 * Find the length of the buffer passed-in in bytes.
2362 bufferSize = len * sizeof (WCHAR);
2365 * Allocate a new buffer to hold the string.
2366 * don't forget to keep an empty spot at the beginning of the
2367 * buffer for the character count and an extra character at the
2370 newBuffer = HeapAlloc(GetProcessHeap(), 0,
2371 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
2374 * If the memory allocation failed, return a null pointer.
2380 * Copy the length of the string in the placeholder.
2382 *newBuffer = bufferSize;
2385 * Skip the byte count.
2390 * Copy the information in the buffer.
2391 * Since it is valid to pass a NULL pointer here, we'll initialize the
2392 * buffer to nul if it is the case.
2395 memcpy(newBuffer, str, bufferSize);
2397 memset(newBuffer, 0, bufferSize);
2400 * Make sure that there is a nul character at the end of the
2403 stringBuffer = (WCHAR*)newBuffer;
2404 stringBuffer[len] = L'\0';
2406 return (LPWSTR)stringBuffer;
2409 /***********************************************************************
2410 * PropSysFreeString [OLE32.@]
2412 * Copy of SysFreeString.
2414 void WINAPI PropSysFreeString(LPOLESTR str)
2416 DWORD* bufferPointer;
2418 /* NULL is a valid parameter */
2422 * We have to be careful when we free a BSTR pointer, it points to
2423 * the beginning of the string but it skips the byte count contained
2424 * before the string.
2426 bufferPointer = (DWORD*)str;
2431 * Free the memory from its "real" origin.
2433 HeapFree(GetProcessHeap(), 0, bufferPointer);
2436 /******************************************************************************
2437 * Check if a PROPVARIANT's type is valid.
2439 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2465 case VT_STREAMED_OBJECT:
2466 case VT_STORED_OBJECT:
2467 case VT_BLOB_OBJECT:
2470 case VT_I2|VT_VECTOR:
2471 case VT_I4|VT_VECTOR:
2472 case VT_R4|VT_VECTOR:
2473 case VT_R8|VT_VECTOR:
2474 case VT_CY|VT_VECTOR:
2475 case VT_DATE|VT_VECTOR:
2476 case VT_BSTR|VT_VECTOR:
2477 case VT_ERROR|VT_VECTOR:
2478 case VT_BOOL|VT_VECTOR:
2479 case VT_VARIANT|VT_VECTOR:
2480 case VT_UI1|VT_VECTOR:
2481 case VT_UI2|VT_VECTOR:
2482 case VT_UI4|VT_VECTOR:
2483 case VT_I8|VT_VECTOR:
2484 case VT_UI8|VT_VECTOR:
2485 case VT_LPSTR|VT_VECTOR:
2486 case VT_LPWSTR|VT_VECTOR:
2487 case VT_FILETIME|VT_VECTOR:
2488 case VT_CF|VT_VECTOR:
2489 case VT_CLSID|VT_VECTOR:
2492 WARN("Bad type %d\n", vt);
2493 return STG_E_INVALIDPARAMETER;
2496 /***********************************************************************
2497 * PropVariantClear [OLE32.@]
2499 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2503 TRACE("(%p)\n", pvar);
2508 hr = PROPVARIANT_ValidateType(pvar->vt);
2515 case VT_STREAMED_OBJECT:
2517 case VT_STORED_OBJECT:
2518 if (pvar->u.pStream)
2519 IUnknown_Release(pvar->u.pStream);
2524 /* pick an arbitary typed pointer - we don't care about the type
2525 * as we are just freeing it */
2526 CoTaskMemFree(pvar->u.puuid);
2529 case VT_BLOB_OBJECT:
2530 CoTaskMemFree(pvar->u.blob.pBlobData);
2533 if (pvar->u.bstrVal)
2534 PropSysFreeString(pvar->u.bstrVal);
2537 if (pvar->u.pclipdata)
2539 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2540 CoTaskMemFree(pvar->u.pclipdata);
2544 if (pvar->vt & VT_VECTOR)
2548 switch (pvar->vt & ~VT_VECTOR)
2551 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2554 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2557 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2558 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2561 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2562 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2565 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2566 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2569 if (pvar->vt & ~VT_VECTOR)
2571 /* pick an arbitary VT_VECTOR structure - they all have the same
2573 CoTaskMemFree(pvar->u.capropvar.pElems);
2577 WARN("Invalid/unsupported type %d\n", pvar->vt);
2580 ZeroMemory(pvar, sizeof(*pvar));
2585 /***********************************************************************
2586 * PropVariantCopy [OLE32.@]
2588 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2589 const PROPVARIANT *pvarSrc) /* [in] */
2594 TRACE("(%p, %p)\n", pvarDest, pvarSrc);
2596 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2600 /* this will deal with most cases */
2601 CopyMemory(pvarDest, pvarSrc, sizeof(*pvarDest));
2606 case VT_STREAMED_OBJECT:
2608 case VT_STORED_OBJECT:
2609 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
2612 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
2613 CopyMemory(pvarDest->u.puuid, pvarSrc->u.puuid, sizeof(CLSID));
2616 len = strlen(pvarSrc->u.pszVal);
2617 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
2618 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
2621 len = lstrlenW(pvarSrc->u.pwszVal);
2622 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
2623 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
2626 case VT_BLOB_OBJECT:
2627 if (pvarSrc->u.blob.pBlobData)
2629 len = pvarSrc->u.blob.cbSize;
2630 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
2631 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
2635 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
2638 if (pvarSrc->u.pclipdata)
2640 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
2641 CoTaskMemAlloc(len);
2642 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
2646 if (pvarSrc->vt & VT_VECTOR)
2651 switch(pvarSrc->vt & ~VT_VECTOR)
2653 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
2654 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
2655 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
2656 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
2657 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
2658 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
2659 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
2660 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
2661 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
2662 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
2663 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
2664 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
2665 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
2666 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
2667 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
2668 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
2669 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
2670 case VT_BSTR: elemSize = sizeof(*pvarSrc->u.bstrVal); break;
2671 case VT_LPSTR: elemSize = sizeof(*pvarSrc->u.pszVal); break;
2672 case VT_LPWSTR: elemSize = sizeof(*pvarSrc->u.pwszVal); break;
2676 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
2677 return E_INVALIDARG;
2679 len = pvarSrc->u.capropvar.cElems;
2680 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
2681 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
2683 for (i = 0; i < len; i++)
2684 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
2686 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
2688 FIXME("Copy clipformats\n");
2690 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
2692 for (i = 0; i < len; i++)
2693 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
2695 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2698 for (i = 0; i < len; i++)
2700 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
2701 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2702 memcpy(pvarDest->u.calpstr.pElems[i],
2703 pvarSrc->u.calpstr.pElems[i], strLen);
2706 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
2709 for (i = 0; i < len; i++)
2711 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
2713 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2714 memcpy(pvarDest->u.calpstr.pElems[i],
2715 pvarSrc->u.calpstr.pElems[i], strLen);
2719 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
2722 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
2728 /***********************************************************************
2729 * FreePropVariantArray [OLE32.@]
2731 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
2732 PROPVARIANT *rgvars) /* [in/out] */
2736 TRACE("(%lu, %p)\n", cVariants, rgvars);
2738 for(i = 0; i < cVariants; i++)
2739 PropVariantClear(&rgvars[i]);
2744 /******************************************************************************
2745 * DllDebugObjectRPCHook (OLE32.@)
2746 * turns on and off internal debugging, pointer is only used on macintosh
2749 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)