mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / ole32 / ole2.c
blobf2224ad5b6a90ebdf098be99711ebcd7d0208215
1 /*
2 * OLE2 library
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
9 * Copyright 2011 Adam Martinson for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #define COBJMACROS
33 #define NONAMELESSUNION
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "winreg.h"
42 #include "ole2.h"
43 #include "ole2ver.h"
45 #include "compobj_private.h"
46 #include "olestd.h"
47 #include "wine/list.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 WINE_DECLARE_DEBUG_CHANNEL(accel);
54 /******************************************************************************
55 * These are static/global variables and internal data structures that the
56 * OLE module uses to maintain its state.
58 typedef struct tagTrackerWindowInfo
60 IDataObject* dataObject;
61 IDropSource* dropSource;
62 DWORD dwOKEffect;
63 DWORD* pdwEffect;
64 BOOL trackingDone;
65 BOOL inTrackCall;
66 HRESULT returnValue;
68 BOOL escPressed;
69 HWND curTargetHWND; /* window the mouse is hovering over */
70 IDropTarget* curDragTarget;
71 POINTL curMousePos; /* current position of the mouse in screen coordinates */
72 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
73 } TrackerWindowInfo;
75 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
77 HWND hwndFrame; /* The containers frame window */
78 HWND hwndActiveObject; /* The active objects window */
79 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
80 HMENU hmenuCombined; /* The combined menu */
81 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
82 } OleMenuDescriptor;
84 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
86 DWORD tid; /* Thread Id */
87 HANDLE hHeap; /* Heap this is allocated from */
88 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
89 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
90 struct tagOleMenuHookItem *next;
91 } OleMenuHookItem;
93 static OleMenuHookItem *hook_list;
96 * This is the lock count on the OLE library. It is controlled by the
97 * OLEInitialize/OLEUninitialize methods.
99 static LONG OLE_moduleLockCount = 0;
102 * Name of our registered window class.
104 static const WCHAR OLEDD_DRAGTRACKERCLASS[] = L"WineDragDropTracker32";
107 * Name of menu descriptor property.
109 static const WCHAR prop_olemenuW[] = L"PROP_OLEMenuDescriptor";
111 /* property to store IDropTarget pointer */
112 static const WCHAR prop_oledroptarget[] = L"OleDropTargetInterface";
114 /* property to store Marshalled IDropTarget pointer */
115 static const WCHAR prop_marshalleddroptarget[] = L"WineMarshalledDropTarget";
117 /******************************************************************************
118 * These are the prototypes of miscellaneous utility methods
120 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
122 /******************************************************************************
123 * These are the prototypes of the utility methods used to manage a shared menu
125 static void OLEMenu_Initialize(void);
126 static void OLEMenu_UnInitialize(void);
127 static BOOL OLEMenu_InstallHooks( DWORD tid );
128 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
129 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
130 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
131 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
132 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
133 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
135 /******************************************************************************
136 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
138 extern void OLEClipbrd_UnInitialize(void);
139 extern void OLEClipbrd_Initialize(void);
141 /******************************************************************************
142 * These are the prototypes of the utility methods used for OLE Drag n Drop
144 static void OLEDD_Initialize(void);
145 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
146 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo);
147 static DWORD OLEDD_GetButtonState(void);
149 /******************************************************************************
150 * OleBuildVersion [OLE32.@]
152 DWORD WINAPI OleBuildVersion(void)
154 TRACE("Returning version %d, build %d.\n", rmm, rup);
155 return (rmm<<16)+rup;
158 /***********************************************************************
159 * OleInitialize (OLE32.@)
161 HRESULT WINAPI DECLSPEC_HOTPATCH OleInitialize(LPVOID reserved)
163 HRESULT hr;
165 TRACE("(%p)\n", reserved);
168 * The first duty of the OleInitialize is to initialize the COM libraries.
170 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
173 * If the CoInitializeEx call failed, the OLE libraries can't be
174 * initialized.
176 if (FAILED(hr))
177 return hr;
179 if (!COM_CurrentInfo()->ole_inits)
180 hr = S_OK;
181 else
182 hr = S_FALSE;
185 * Then, it has to initialize the OLE specific modules.
186 * This includes:
187 * Clipboard
188 * Drag and Drop
189 * Object linking and Embedding
190 * In-place activation
192 if (!COM_CurrentInfo()->ole_inits++ &&
193 InterlockedIncrement(&OLE_moduleLockCount) == 1)
196 * Initialize the libraries.
198 TRACE("() - Initializing the OLE libraries\n");
201 * OLE Clipboard
203 OLEClipbrd_Initialize();
206 * Drag and Drop
208 OLEDD_Initialize();
211 * OLE shared menu
213 OLEMenu_Initialize();
216 return hr;
219 /******************************************************************************
220 * OleUninitialize [OLE32.@]
222 void WINAPI DECLSPEC_HOTPATCH OleUninitialize(void)
224 TRACE("()\n");
226 if (COM_CurrentInfo()->ole_inits == 0)
228 WARN("ole_inits is already 0\n");
229 return ;
232 * If we hit the bottom of the lock stack, free the libraries.
234 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
237 * Actually free the libraries.
239 TRACE("() - Freeing the last reference count\n");
242 * OLE Clipboard
244 OLEClipbrd_UnInitialize();
247 * OLE shared menu
249 OLEMenu_UnInitialize();
253 * Then, uninitialize the COM libraries.
255 CoUninitialize();
258 /******************************************************************************
259 * OleInitializeWOW [OLE32.@]
261 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
262 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
263 return 0;
266 /*************************************************************
267 * get_droptarget_handle
269 * Retrieve a handle to the map containing the marshalled IDropTarget.
270 * This handle belongs to the process that called RegisterDragDrop.
271 * See get_droptarget_local_handle().
273 static inline HANDLE get_droptarget_handle(HWND hwnd)
275 return GetPropW(hwnd, prop_marshalleddroptarget);
278 /*************************************************************
279 * is_droptarget
281 * Is the window a droptarget.
283 static inline BOOL is_droptarget(HWND hwnd)
285 return get_droptarget_handle(hwnd) != 0;
288 /*************************************************************
289 * get_droptarget_local_handle
291 * Retrieve a handle to the map containing the marshalled IDropTarget.
292 * The handle should be closed when finished with.
294 static HANDLE get_droptarget_local_handle(HWND hwnd)
296 HANDLE handle, local_handle = 0;
298 handle = get_droptarget_handle(hwnd);
300 if(handle)
302 DWORD pid;
303 HANDLE process;
305 GetWindowThreadProcessId(hwnd, &pid);
306 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
307 if(process)
309 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
310 CloseHandle(process);
313 return local_handle;
316 /***********************************************************************
317 * create_map_from_stream
319 * Helper for RegisterDragDrop. Creates a file mapping object
320 * with the contents of the provided stream. The stream must
321 * be a global memory backed stream.
323 static HRESULT create_map_from_stream(IStream *stream, HANDLE *map)
325 HGLOBAL hmem;
326 DWORD size;
327 HRESULT hr;
328 void *data;
330 hr = GetHGlobalFromStream(stream, &hmem);
331 if(FAILED(hr)) return hr;
333 size = GlobalSize(hmem);
334 *map = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, NULL);
335 if(!*map) return E_OUTOFMEMORY;
337 data = MapViewOfFile(*map, FILE_MAP_WRITE, 0, 0, size);
338 memcpy(data, GlobalLock(hmem), size);
339 GlobalUnlock(hmem);
340 UnmapViewOfFile(data);
341 return S_OK;
344 /***********************************************************************
345 * create_stream_from_map
347 * Creates a stream from the provided map.
349 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
351 HRESULT hr = E_OUTOFMEMORY;
352 HGLOBAL hmem;
353 void *data;
354 MEMORY_BASIC_INFORMATION info;
356 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
357 if(!data) return hr;
359 VirtualQuery(data, &info, sizeof(info));
360 TRACE("size %d\n", (int)info.RegionSize);
362 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
363 if(hmem)
365 memcpy(GlobalLock(hmem), data, info.RegionSize);
366 GlobalUnlock(hmem);
367 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
369 UnmapViewOfFile(data);
370 return hr;
373 /* This is to work around apps which break COM rules by not implementing
374 * IDropTarget::QueryInterface(). Windows doesn't expose this because it
375 * doesn't call CoMarshallInterface() in RegisterDragDrop().
376 * The wrapper is only used internally, and only exists for the life of
377 * the marshal. We don't want to hold a ref on the app provided target
378 * as some apps destroy this prior to CoUninitialize without calling
379 * RevokeDragDrop. The only (long-term) ref is held by the window prop. */
380 typedef struct {
381 IDropTarget IDropTarget_iface;
382 HWND hwnd;
383 LONG refs;
384 } DropTargetWrapper;
386 static inline DropTargetWrapper* impl_from_IDropTarget(IDropTarget* iface)
388 return CONTAINING_RECORD(iface, DropTargetWrapper, IDropTarget_iface);
391 static HRESULT WINAPI DropTargetWrapper_QueryInterface(IDropTarget* iface,
392 REFIID riid,
393 void** ppvObject)
395 DropTargetWrapper* This = impl_from_IDropTarget(iface);
396 if (IsEqualIID(riid, &IID_IUnknown) ||
397 IsEqualIID(riid, &IID_IDropTarget))
399 IDropTarget_AddRef(&This->IDropTarget_iface);
400 *ppvObject = &This->IDropTarget_iface;
401 return S_OK;
403 *ppvObject = NULL;
404 return E_NOINTERFACE;
407 static ULONG WINAPI DropTargetWrapper_AddRef(IDropTarget* iface)
409 DropTargetWrapper* This = impl_from_IDropTarget(iface);
410 return InterlockedIncrement(&This->refs);
413 static ULONG WINAPI DropTargetWrapper_Release(IDropTarget* iface)
415 DropTargetWrapper* This = impl_from_IDropTarget(iface);
416 ULONG refs = InterlockedDecrement(&This->refs);
417 if (!refs) HeapFree(GetProcessHeap(), 0, This);
418 return refs;
421 static inline HRESULT get_target_from_wrapper( IDropTarget *wrapper, IDropTarget **target )
423 DropTargetWrapper* This = impl_from_IDropTarget( wrapper );
424 *target = GetPropW( This->hwnd, prop_oledroptarget );
425 if (!*target) return DRAGDROP_E_NOTREGISTERED;
426 IDropTarget_AddRef( *target );
427 return S_OK;
430 static HRESULT WINAPI DropTargetWrapper_DragEnter(IDropTarget* iface,
431 IDataObject* pDataObj,
432 DWORD grfKeyState,
433 POINTL pt,
434 DWORD* pdwEffect)
436 IDropTarget *target;
437 HRESULT r = get_target_from_wrapper( iface, &target );
439 if (SUCCEEDED( r ))
441 r = IDropTarget_DragEnter( target, pDataObj, grfKeyState, pt, pdwEffect );
442 IDropTarget_Release( target );
444 return r;
447 static HRESULT WINAPI DropTargetWrapper_DragOver(IDropTarget* iface,
448 DWORD grfKeyState,
449 POINTL pt,
450 DWORD* pdwEffect)
452 IDropTarget *target;
453 HRESULT r = get_target_from_wrapper( iface, &target );
455 if (SUCCEEDED( r ))
457 r = IDropTarget_DragOver( target, grfKeyState, pt, pdwEffect );
458 IDropTarget_Release( target );
460 return r;
463 static HRESULT WINAPI DropTargetWrapper_DragLeave(IDropTarget* iface)
465 IDropTarget *target;
466 HRESULT r = get_target_from_wrapper( iface, &target );
468 if (SUCCEEDED( r ))
470 r = IDropTarget_DragLeave( target );
471 IDropTarget_Release( target );
473 return r;
476 static HRESULT WINAPI DropTargetWrapper_Drop(IDropTarget* iface,
477 IDataObject* pDataObj,
478 DWORD grfKeyState,
479 POINTL pt,
480 DWORD* pdwEffect)
482 IDropTarget *target;
483 HRESULT r = get_target_from_wrapper( iface, &target );
485 if (SUCCEEDED( r ))
487 r = IDropTarget_Drop( target, pDataObj, grfKeyState, pt, pdwEffect );
488 IDropTarget_Release( target );
490 return r;
493 static const IDropTargetVtbl DropTargetWrapperVTbl =
495 DropTargetWrapper_QueryInterface,
496 DropTargetWrapper_AddRef,
497 DropTargetWrapper_Release,
498 DropTargetWrapper_DragEnter,
499 DropTargetWrapper_DragOver,
500 DropTargetWrapper_DragLeave,
501 DropTargetWrapper_Drop
504 static IDropTarget* WrapDropTarget( HWND hwnd )
506 DropTargetWrapper* This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
508 if (This)
510 This->IDropTarget_iface.lpVtbl = &DropTargetWrapperVTbl;
511 This->hwnd = hwnd;
512 This->refs = 1;
514 return &This->IDropTarget_iface;
517 /***********************************************************************
518 * get_droptarget_pointer
520 * Retrieves the marshalled IDropTarget from the window.
522 static IDropTarget* get_droptarget_pointer(HWND hwnd)
524 IDropTarget *droptarget = NULL;
525 HANDLE map;
526 IStream *stream;
528 map = get_droptarget_local_handle(hwnd);
529 if(!map) return NULL;
531 if(SUCCEEDED(create_stream_from_map(map, &stream)))
533 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
534 IStream_Release(stream);
536 CloseHandle(map);
537 return droptarget;
540 /***********************************************************************
541 * RegisterDragDrop (OLE32.@)
543 HRESULT WINAPI RegisterDragDrop(HWND hwnd, LPDROPTARGET pDropTarget)
545 DWORD pid = 0;
546 HRESULT hr;
547 IStream *stream;
548 HANDLE map;
549 IDropTarget *wrapper;
551 TRACE("(%p,%p)\n", hwnd, pDropTarget);
553 if (!COM_CurrentApt())
555 ERR("COM not initialized\n");
556 return E_OUTOFMEMORY;
559 if (!pDropTarget)
560 return E_INVALIDARG;
562 if (!IsWindow(hwnd))
564 ERR("invalid hwnd %p\n", hwnd);
565 return DRAGDROP_E_INVALIDHWND;
568 /* block register for other processes windows */
569 GetWindowThreadProcessId(hwnd, &pid);
570 if (pid != GetCurrentProcessId())
572 FIXME("register for another process windows is disabled\n");
573 return DRAGDROP_E_INVALIDHWND;
576 /* check if the window is already registered */
577 if (is_droptarget(hwnd))
578 return DRAGDROP_E_ALREADYREGISTERED;
581 * Marshal the drop target pointer into a shared memory map and
582 * store the map's handle in a Wine specific window prop. We also
583 * store the drop target pointer itself in the
584 * "OleDropTargetInterface" prop for compatibility with Windows.
587 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
588 if(FAILED(hr)) return hr;
590 /* IDropTarget::QueryInterface() shouldn't be called, some (broken) apps depend on this. */
591 wrapper = WrapDropTarget( hwnd );
592 if(!wrapper)
594 IStream_Release(stream);
595 return E_OUTOFMEMORY;
597 hr = CoMarshalInterface(stream, &IID_IDropTarget, (IUnknown*)wrapper, MSHCTX_LOCAL, NULL, MSHLFLAGS_TABLESTRONG);
598 IDropTarget_Release(wrapper);
600 if(SUCCEEDED(hr))
602 hr = create_map_from_stream(stream, &map);
603 if(SUCCEEDED(hr))
605 IDropTarget_AddRef(pDropTarget);
606 SetPropW(hwnd, prop_oledroptarget, pDropTarget);
607 SetPropW(hwnd, prop_marshalleddroptarget, map);
609 else
611 LARGE_INTEGER zero;
612 zero.QuadPart = 0;
613 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
614 CoReleaseMarshalData(stream);
617 IStream_Release(stream);
619 return hr;
622 /***********************************************************************
623 * RevokeDragDrop (OLE32.@)
625 HRESULT WINAPI RevokeDragDrop(HWND hwnd)
627 HANDLE map;
628 IStream *stream;
629 IDropTarget *drop_target;
630 HRESULT hr;
632 TRACE("(%p)\n", hwnd);
634 if (!IsWindow(hwnd))
636 ERR("invalid hwnd %p\n", hwnd);
637 return DRAGDROP_E_INVALIDHWND;
640 /* no registration data */
641 if (!(map = get_droptarget_handle(hwnd)))
642 return DRAGDROP_E_NOTREGISTERED;
644 drop_target = GetPropW(hwnd, prop_oledroptarget);
645 if(drop_target) IDropTarget_Release(drop_target);
647 RemovePropW(hwnd, prop_oledroptarget);
648 RemovePropW(hwnd, prop_marshalleddroptarget);
650 hr = create_stream_from_map(map, &stream);
651 if(SUCCEEDED(hr))
653 CoReleaseMarshalData(stream);
654 IStream_Release(stream);
656 CloseHandle(map);
658 return hr;
661 /***********************************************************************
662 * OleRegGetUserType (OLE32.@)
664 HRESULT WINAPI OleRegGetUserType(REFCLSID clsid, DWORD form, LPOLESTR *usertype)
666 DWORD valuetype, valuelen;
667 WCHAR auxkeynameW[16];
668 HKEY usertypekey;
669 HRESULT hres;
670 LONG ret;
672 TRACE("(%s, %u, %p)\n", debugstr_guid(clsid), form, usertype);
674 if (!usertype)
675 return E_INVALIDARG;
677 *usertype = NULL;
679 /* Return immediately if it's not registered. */
680 hres = COM_OpenKeyForCLSID(clsid, NULL, KEY_READ, &usertypekey);
681 if (FAILED(hres))
682 return hres;
684 valuelen = 0;
686 /* Try additional types if requested. If they don't exist fall back to USERCLASSTYPE_FULL. */
687 if (form != USERCLASSTYPE_FULL)
689 HKEY auxkey;
691 swprintf(auxkeynameW, ARRAY_SIZE(auxkeynameW), L"AuxUserType\\%d", form);
692 if (COM_OpenKeyForCLSID(clsid, auxkeynameW, KEY_READ, &auxkey) == S_OK)
694 if (!RegQueryValueExW(auxkey, L"", NULL, &valuetype, NULL, &valuelen) && valuelen)
696 RegCloseKey(usertypekey);
697 usertypekey = auxkey;
699 else
700 RegCloseKey(auxkey);
704 valuelen = 0;
705 if (RegQueryValueExW(usertypekey, L"", NULL, &valuetype, NULL, &valuelen))
707 RegCloseKey(usertypekey);
708 return REGDB_E_READREGDB;
711 *usertype = CoTaskMemAlloc(valuelen);
712 if (!*usertype)
714 RegCloseKey(usertypekey);
715 return E_OUTOFMEMORY;
718 ret = RegQueryValueExW(usertypekey, L"", NULL, &valuetype, (BYTE *)*usertype, &valuelen);
719 RegCloseKey(usertypekey);
720 if (ret != ERROR_SUCCESS)
722 CoTaskMemFree(*usertype);
723 *usertype = NULL;
724 return REGDB_E_READREGDB;
727 return S_OK;
730 /***********************************************************************
731 * DoDragDrop [OLE32.@]
733 HRESULT WINAPI DoDragDrop (
734 IDataObject *pDataObject, /* [in] ptr to the data obj */
735 IDropSource* pDropSource, /* [in] ptr to the source obj */
736 DWORD dwOKEffect, /* [in] effects allowed by the source */
737 DWORD *pdwEffect) /* [out] ptr to effects of the source */
739 TrackerWindowInfo trackerInfo;
740 HWND hwndTrackWindow;
741 MSG msg;
743 TRACE("(%p, %p, %08x, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
745 if (!pDataObject || !pDropSource || !pdwEffect)
746 return E_INVALIDARG;
749 * Setup the drag n drop tracking window.
752 trackerInfo.dataObject = pDataObject;
753 trackerInfo.dropSource = pDropSource;
754 trackerInfo.dwOKEffect = dwOKEffect;
755 trackerInfo.pdwEffect = pdwEffect;
756 trackerInfo.trackingDone = FALSE;
757 trackerInfo.inTrackCall = FALSE;
758 trackerInfo.escPressed = FALSE;
759 trackerInfo.curTargetHWND = 0;
760 trackerInfo.curDragTarget = 0;
762 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, L"TrackerWindow",
763 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
764 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
765 &trackerInfo);
767 if (hwndTrackWindow)
770 * Capture the mouse input
772 SetCapture(hwndTrackWindow);
774 msg.message = 0;
777 * Pump messages. All mouse input should go to the capture window.
779 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
781 trackerInfo.curMousePos.x = msg.pt.x;
782 trackerInfo.curMousePos.y = msg.pt.y;
783 trackerInfo.dwKeyState = OLEDD_GetButtonState();
785 if ( (msg.message >= WM_KEYFIRST) &&
786 (msg.message <= WM_KEYLAST) )
789 * When keyboard messages are sent to windows on this thread, we
790 * want to ignore notify the drop source that the state changed.
791 * in the case of the Escape key, we also notify the drop source
792 * we give it a special meaning.
794 if ( (msg.message==WM_KEYDOWN) &&
795 (msg.wParam==VK_ESCAPE) )
797 trackerInfo.escPressed = TRUE;
801 * Notify the drop source.
803 OLEDD_TrackStateChange(&trackerInfo);
805 else
808 * Dispatch the messages only when it's not a keyboard message.
810 DispatchMessageW(&msg);
814 /* re-post the quit message to outer message loop */
815 if (msg.message == WM_QUIT)
816 PostQuitMessage(msg.wParam);
818 * Destroy the temporary window.
820 DestroyWindow(hwndTrackWindow);
822 return trackerInfo.returnValue;
825 return E_FAIL;
828 /***********************************************************************
829 * OleQueryLinkFromData [OLE32.@]
831 HRESULT WINAPI OleQueryLinkFromData(
832 IDataObject* pSrcDataObject)
834 FIXME("(%p),stub!\n", pSrcDataObject);
835 return S_FALSE;
838 /***********************************************************************
839 * OleRegGetMiscStatus [OLE32.@]
841 HRESULT WINAPI OleRegGetMiscStatus(
842 REFCLSID clsid,
843 DWORD dwAspect,
844 DWORD* pdwStatus)
846 WCHAR keyName[16];
847 HKEY miscStatusKey;
848 HKEY aspectKey;
849 LONG result;
850 HRESULT hr;
852 TRACE("(%s, %d, %p)\n", debugstr_guid(clsid), dwAspect, pdwStatus);
854 if (!pdwStatus) return E_INVALIDARG;
856 *pdwStatus = 0;
858 if (actctx_get_miscstatus(clsid, dwAspect, pdwStatus)) return S_OK;
860 hr = COM_OpenKeyForCLSID(clsid, L"MiscStatus", KEY_READ, &miscStatusKey);
861 if (FAILED(hr))
862 /* missing key is not a failure */
863 return hr == REGDB_E_KEYMISSING ? S_OK : hr;
865 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
868 * Open the key specific to the requested aspect.
870 swprintf(keyName, ARRAY_SIZE(keyName), L"%d", dwAspect);
872 result = open_classes_key(miscStatusKey, keyName, KEY_READ, &aspectKey);
873 if (result == ERROR_SUCCESS)
875 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
876 RegCloseKey(aspectKey);
879 RegCloseKey(miscStatusKey);
880 return S_OK;
883 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
885 typedef struct
887 IEnumOLEVERB IEnumOLEVERB_iface;
888 LONG ref;
890 HKEY hkeyVerb;
891 ULONG index;
892 } EnumOLEVERB;
894 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
896 return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
899 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
900 IEnumOLEVERB *iface, REFIID riid, void **ppv)
902 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
903 if (IsEqualIID(riid, &IID_IUnknown) ||
904 IsEqualIID(riid, &IID_IEnumOLEVERB))
906 IEnumOLEVERB_AddRef(iface);
907 *ppv = iface;
908 return S_OK;
910 return E_NOINTERFACE;
913 static ULONG WINAPI EnumOLEVERB_AddRef(
914 IEnumOLEVERB *iface)
916 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
917 TRACE("()\n");
918 return InterlockedIncrement(&This->ref);
921 static ULONG WINAPI EnumOLEVERB_Release(
922 IEnumOLEVERB *iface)
924 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
925 LONG refs = InterlockedDecrement(&This->ref);
926 TRACE("()\n");
927 if (!refs)
929 RegCloseKey(This->hkeyVerb);
930 HeapFree(GetProcessHeap(), 0, This);
932 return refs;
935 static HRESULT WINAPI EnumOLEVERB_Next(
936 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
937 ULONG *pceltFetched)
939 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
940 HRESULT hr = S_OK;
942 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
944 if (pceltFetched)
945 *pceltFetched = 0;
947 for (; celt; celt--, rgelt++)
949 WCHAR wszSubKey[20];
950 LONG cbData;
951 LPWSTR pwszOLEVERB;
952 LPWSTR pwszMenuFlags;
953 LPWSTR pwszAttribs;
954 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, ARRAY_SIZE(wszSubKey));
955 if (res == ERROR_NO_MORE_ITEMS)
957 hr = S_FALSE;
958 break;
960 else if (res != ERROR_SUCCESS)
962 ERR("RegEnumKeyW failed with error %d\n", res);
963 hr = REGDB_E_READREGDB;
964 break;
966 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
967 if (res != ERROR_SUCCESS)
969 ERR("RegQueryValueW failed with error %d\n", res);
970 hr = REGDB_E_READREGDB;
971 break;
973 pwszOLEVERB = CoTaskMemAlloc(cbData);
974 if (!pwszOLEVERB)
976 hr = E_OUTOFMEMORY;
977 break;
979 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
980 if (res != ERROR_SUCCESS)
982 ERR("RegQueryValueW failed with error %d\n", res);
983 hr = REGDB_E_READREGDB;
984 CoTaskMemFree(pwszOLEVERB);
985 break;
988 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
989 pwszMenuFlags = wcschr(pwszOLEVERB, ',');
990 if (!pwszMenuFlags)
992 hr = OLEOBJ_E_INVALIDVERB;
993 CoTaskMemFree(pwszOLEVERB);
994 break;
996 /* nul terminate the name string and advance to first character */
997 *pwszMenuFlags = '\0';
998 pwszMenuFlags++;
999 pwszAttribs = wcschr(pwszMenuFlags, ',');
1000 if (!pwszAttribs)
1002 hr = OLEOBJ_E_INVALIDVERB;
1003 CoTaskMemFree(pwszOLEVERB);
1004 break;
1006 /* nul terminate the menu string and advance to first character */
1007 *pwszAttribs = '\0';
1008 pwszAttribs++;
1010 /* fill out structure for this verb */
1011 rgelt->lVerb = wcstol(wszSubKey, NULL, 10);
1012 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
1013 rgelt->fuFlags = wcstol(pwszMenuFlags, NULL, 10);
1014 rgelt->grfAttribs = wcstol(pwszAttribs, NULL, 10);
1016 if (pceltFetched)
1017 (*pceltFetched)++;
1018 This->index++;
1020 return hr;
1023 static HRESULT WINAPI EnumOLEVERB_Skip(
1024 IEnumOLEVERB *iface, ULONG celt)
1026 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1028 TRACE("(%d)\n", celt);
1030 This->index += celt;
1031 return S_OK;
1034 static HRESULT WINAPI EnumOLEVERB_Reset(
1035 IEnumOLEVERB *iface)
1037 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1039 TRACE("()\n");
1041 This->index = 0;
1042 return S_OK;
1045 static HRESULT WINAPI EnumOLEVERB_Clone(
1046 IEnumOLEVERB *iface,
1047 IEnumOLEVERB **ppenum)
1049 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1050 HKEY hkeyVerb;
1051 TRACE("(%p)\n", ppenum);
1052 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
1053 return HRESULT_FROM_WIN32(GetLastError());
1054 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
1057 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
1059 EnumOLEVERB_QueryInterface,
1060 EnumOLEVERB_AddRef,
1061 EnumOLEVERB_Release,
1062 EnumOLEVERB_Next,
1063 EnumOLEVERB_Skip,
1064 EnumOLEVERB_Reset,
1065 EnumOLEVERB_Clone
1068 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
1070 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1071 if (!This)
1073 RegCloseKey(hkeyVerb);
1074 return E_OUTOFMEMORY;
1076 This->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERB_VTable;
1077 This->ref = 1;
1078 This->index = index;
1079 This->hkeyVerb = hkeyVerb;
1080 *ppenum = &This->IEnumOLEVERB_iface;
1081 return S_OK;
1084 /***********************************************************************
1085 * OleRegEnumVerbs [OLE32.@]
1087 * Enumerates verbs associated with a class stored in the registry.
1089 * PARAMS
1090 * clsid [I] Class ID to enumerate the verbs for.
1091 * ppenum [O] Enumerator.
1093 * RETURNS
1094 * S_OK: Success.
1095 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1096 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1097 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1098 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1100 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
1102 LONG res;
1103 HKEY hkeyVerb;
1104 DWORD dwSubKeys;
1106 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
1108 res = COM_OpenKeyForCLSID(clsid, L"Verb", KEY_READ, &hkeyVerb);
1109 if (FAILED(res))
1111 if (res == REGDB_E_CLASSNOTREG)
1112 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
1113 else if (res == REGDB_E_KEYMISSING)
1114 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
1115 else
1116 ERR("failed to open Verbs key for CLSID %s with error %d\n",
1117 debugstr_guid(clsid), res);
1118 return res;
1121 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
1122 NULL, NULL, NULL, NULL, NULL, NULL);
1123 if (res != ERROR_SUCCESS)
1125 ERR("failed to get subkey count with error %d\n", GetLastError());
1126 return REGDB_E_READREGDB;
1129 if (!dwSubKeys)
1131 WARN("class %s has no verbs\n", debugstr_guid(clsid));
1132 RegCloseKey(hkeyVerb);
1133 return OLEOBJ_E_NOVERBS;
1136 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
1139 /******************************************************************************
1140 * OleSetContainedObject [OLE32.@]
1142 HRESULT WINAPI OleSetContainedObject(
1143 LPUNKNOWN pUnknown,
1144 BOOL fContained)
1146 IRunnableObject* runnable = NULL;
1147 HRESULT hres;
1149 TRACE("(%p,%x)\n", pUnknown, fContained);
1151 hres = IUnknown_QueryInterface(pUnknown,
1152 &IID_IRunnableObject,
1153 (void**)&runnable);
1155 if (SUCCEEDED(hres))
1157 hres = IRunnableObject_SetContainedObject(runnable, fContained);
1159 IRunnableObject_Release(runnable);
1161 return hres;
1164 return S_OK;
1167 /******************************************************************************
1168 * OleRun [OLE32.@]
1170 * Set the OLE object to the running state.
1172 * PARAMS
1173 * pUnknown [I] OLE object to run.
1175 * RETURNS
1176 * Success: S_OK.
1177 * Failure: Any HRESULT code.
1179 HRESULT WINAPI DECLSPEC_HOTPATCH OleRun(LPUNKNOWN pUnknown)
1181 IRunnableObject *runable;
1182 HRESULT hres;
1184 TRACE("(%p)\n", pUnknown);
1186 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
1187 if (FAILED(hres))
1188 return S_OK; /* Appears to return no error. */
1190 hres = IRunnableObject_Run(runable, NULL);
1191 IRunnableObject_Release(runable);
1192 return hres;
1195 /******************************************************************************
1196 * OleLoad [OLE32.@]
1198 HRESULT WINAPI OleLoad(
1199 LPSTORAGE pStg,
1200 REFIID riid,
1201 LPOLECLIENTSITE pClientSite,
1202 LPVOID* ppvObj)
1204 IPersistStorage* persistStorage = NULL;
1205 IUnknown* pUnk;
1206 IOleObject* pOleObject = NULL;
1207 STATSTG storageInfo;
1208 HRESULT hres;
1210 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
1212 *ppvObj = NULL;
1215 * TODO, Conversion ... OleDoAutoConvert
1219 * Get the class ID for the object.
1221 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1222 if (FAILED(hres))
1223 return hres;
1226 * Now, try and create the handler for the object
1228 hres = CoCreateInstance(&storageInfo.clsid,
1229 NULL,
1230 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1231 riid,
1232 (void**)&pUnk);
1235 * If that fails, as it will most times, load the default
1236 * OLE handler.
1238 if (FAILED(hres))
1240 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1241 NULL,
1242 riid,
1243 (void**)&pUnk);
1247 * If we couldn't find a handler... this is bad. Abort the whole thing.
1249 if (FAILED(hres))
1250 return hres;
1252 if (pClientSite)
1254 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1255 if (SUCCEEDED(hres))
1257 DWORD dwStatus;
1258 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1263 * Initialize the object with its IPersistStorage interface.
1265 hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (void**)&persistStorage);
1266 if (SUCCEEDED(hres))
1268 hres = IPersistStorage_Load(persistStorage, pStg);
1270 IPersistStorage_Release(persistStorage);
1271 persistStorage = NULL;
1274 if (SUCCEEDED(hres) && pClientSite)
1276 * Inform the new object of its client site.
1278 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1281 * Cleanup interfaces used internally
1283 if (pOleObject)
1284 IOleObject_Release(pOleObject);
1286 if (SUCCEEDED(hres))
1288 IOleLink *pOleLink;
1289 HRESULT hres1;
1290 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1291 if (SUCCEEDED(hres1))
1293 FIXME("handle OLE link\n");
1294 IOleLink_Release(pOleLink);
1298 if (FAILED(hres))
1300 IUnknown_Release(pUnk);
1301 pUnk = NULL;
1304 *ppvObj = pUnk;
1306 return hres;
1309 /***********************************************************************
1310 * OleSave [OLE32.@]
1312 HRESULT WINAPI OleSave(
1313 LPPERSISTSTORAGE pPS,
1314 LPSTORAGE pStg,
1315 BOOL fSameAsLoad)
1317 HRESULT hres;
1318 CLSID objectClass;
1320 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1323 * First, we transfer the class ID (if available)
1325 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1327 if (SUCCEEDED(hres))
1329 WriteClassStg(pStg, &objectClass);
1333 * Then, we ask the object to save itself to the
1334 * storage. If it is successful, we commit the storage.
1336 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1338 if (SUCCEEDED(hres))
1340 IStorage_Commit(pStg,
1341 STGC_DEFAULT);
1344 return hres;
1348 /******************************************************************************
1349 * OleLockRunning [OLE32.@]
1351 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1353 IRunnableObject* runnable = NULL;
1354 HRESULT hres;
1356 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1358 hres = IUnknown_QueryInterface(pUnknown,
1359 &IID_IRunnableObject,
1360 (void**)&runnable);
1362 if (SUCCEEDED(hres))
1364 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1366 IRunnableObject_Release(runnable);
1368 return hres;
1371 return S_OK;
1375 /**************************************************************************
1376 * Internal methods to manage the shared OLE menu in response to the
1377 * OLE***MenuDescriptor API
1380 /***
1381 * OLEMenu_Initialize()
1383 * Initializes the OLEMENU data structures.
1385 static void OLEMenu_Initialize(void)
1389 /***
1390 * OLEMenu_UnInitialize()
1392 * Releases the OLEMENU data structures.
1394 static void OLEMenu_UnInitialize(void)
1398 /*************************************************************************
1399 * OLEMenu_InstallHooks
1400 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1402 * RETURNS: TRUE if message hooks were successfully installed
1403 * FALSE on failure
1405 static BOOL OLEMenu_InstallHooks( DWORD tid )
1407 OleMenuHookItem *pHookItem;
1409 /* Create an entry for the hook table */
1410 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1411 sizeof(OleMenuHookItem)) ) )
1412 return FALSE;
1414 pHookItem->tid = tid;
1415 pHookItem->hHeap = GetProcessHeap();
1416 pHookItem->CallWndProc_hHook = NULL;
1418 /* Install a thread scope message hook for WH_GETMESSAGE */
1419 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1420 0, GetCurrentThreadId() );
1421 if ( !pHookItem->GetMsg_hHook )
1422 goto CLEANUP;
1424 /* Install a thread scope message hook for WH_CALLWNDPROC */
1425 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1426 0, GetCurrentThreadId() );
1427 if ( !pHookItem->CallWndProc_hHook )
1428 goto CLEANUP;
1430 /* Insert the hook table entry */
1431 pHookItem->next = hook_list;
1432 hook_list = pHookItem;
1434 return TRUE;
1436 CLEANUP:
1437 /* Unhook any hooks */
1438 if ( pHookItem->GetMsg_hHook )
1439 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1440 if ( pHookItem->CallWndProc_hHook )
1441 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1442 /* Release the hook table entry */
1443 HeapFree(pHookItem->hHeap, 0, pHookItem );
1445 return FALSE;
1448 /*************************************************************************
1449 * OLEMenu_UnInstallHooks
1450 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1452 * RETURNS: TRUE if message hooks were successfully installed
1453 * FALSE on failure
1455 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1457 OleMenuHookItem *pHookItem = NULL;
1458 OleMenuHookItem **ppHook = &hook_list;
1460 while (*ppHook)
1462 if ((*ppHook)->tid == tid)
1464 pHookItem = *ppHook;
1465 *ppHook = pHookItem->next;
1466 break;
1468 ppHook = &(*ppHook)->next;
1470 if (!pHookItem) return FALSE;
1472 /* Uninstall the hooks installed for this thread */
1473 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1474 goto CLEANUP;
1475 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1476 goto CLEANUP;
1478 /* Release the hook table entry */
1479 HeapFree(pHookItem->hHeap, 0, pHookItem );
1481 return TRUE;
1483 CLEANUP:
1484 /* Release the hook table entry */
1485 HeapFree(pHookItem->hHeap, 0, pHookItem );
1487 return FALSE;
1490 /*************************************************************************
1491 * OLEMenu_IsHookInstalled
1492 * Tests if OLEMenu hooks have been installed for a thread
1494 * RETURNS: The pointer and index of the hook table entry for the tid
1495 * NULL and -1 for the index if no hooks were installed for this thread
1497 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1499 OleMenuHookItem *pHookItem;
1501 /* Do a simple linear search for an entry whose tid matches ours.
1502 * We really need a map but efficiency is not a concern here. */
1503 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1505 if ( tid == pHookItem->tid )
1506 return pHookItem;
1509 return NULL;
1512 /***********************************************************************
1513 * OLEMenu_FindMainMenuIndex
1515 * Used by OLEMenu API to find the top level group a menu item belongs to.
1516 * On success pnPos contains the index of the item in the top level menu group
1518 * RETURNS: TRUE if the ID was found, FALSE on failure
1520 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1522 INT i, nItems;
1524 nItems = GetMenuItemCount( hMainMenu );
1526 for (i = 0; i < nItems; i++)
1528 HMENU hsubmenu;
1530 /* Is the current item a submenu? */
1531 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1533 /* If the handle is the same we're done */
1534 if ( hsubmenu == hPopupMenu )
1536 if (pnPos)
1537 *pnPos = i;
1538 return TRUE;
1540 /* Recursively search without updating pnPos */
1541 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1543 if (pnPos)
1544 *pnPos = i;
1545 return TRUE;
1550 return FALSE;
1553 /***********************************************************************
1554 * OLEMenu_SetIsServerMenu
1556 * Checks whether a popup menu belongs to a shared menu group which is
1557 * owned by the server, and sets the menu descriptor state accordingly.
1558 * All menu messages from these groups should be routed to the server.
1560 * RETURNS: TRUE if the popup menu is part of a server owned group
1561 * FALSE if the popup menu is part of a container owned group
1563 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1565 UINT nPos = 0, nWidth, i;
1567 pOleMenuDescriptor->bIsServerItem = FALSE;
1569 /* Don't bother searching if the popup is the combined menu itself */
1570 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1571 return FALSE;
1573 /* Find the menu item index in the shared OLE menu that this item belongs to */
1574 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1575 return FALSE;
1577 /* The group widths array has counts for the number of elements
1578 * in the groups File, Edit, Container, Object, Window, Help.
1579 * The Edit, Object & Help groups belong to the server object
1580 * and the other three belong to the container.
1581 * Loop through the group widths and locate the group we are a member of.
1583 for ( i = 0, nWidth = 0; i < 6; i++ )
1585 nWidth += pOleMenuDescriptor->mgw.width[i];
1586 if ( nPos < nWidth )
1588 /* Odd elements are server menu widths */
1589 pOleMenuDescriptor->bIsServerItem = i%2;
1590 break;
1594 return pOleMenuDescriptor->bIsServerItem;
1597 /*************************************************************************
1598 * OLEMenu_CallWndProc
1599 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1600 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1602 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1604 LPCWPSTRUCT pMsg;
1605 HOLEMENU hOleMenu = 0;
1606 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1607 OleMenuHookItem *pHookItem = NULL;
1608 WORD fuFlags;
1610 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1612 /* Check if we're being asked to process the message */
1613 if ( HC_ACTION != code )
1614 goto NEXTHOOK;
1616 /* Retrieve the current message being dispatched from lParam */
1617 pMsg = (LPCWPSTRUCT)lParam;
1619 /* Check if the message is destined for a window we are interested in:
1620 * If the window has an OLEMenu property we may need to dispatch
1621 * the menu message to its active objects window instead. */
1623 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1624 if ( !hOleMenu )
1625 goto NEXTHOOK;
1627 /* Get the menu descriptor */
1628 pOleMenuDescriptor = GlobalLock( hOleMenu );
1629 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1630 goto NEXTHOOK;
1632 /* Process menu messages */
1633 switch( pMsg->message )
1635 case WM_INITMENU:
1637 /* Reset the menu descriptor state */
1638 pOleMenuDescriptor->bIsServerItem = FALSE;
1640 /* Send this message to the server as well */
1641 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1642 pMsg->message, pMsg->wParam, pMsg->lParam );
1643 goto NEXTHOOK;
1646 case WM_INITMENUPOPUP:
1648 /* Save the state for whether this is a server owned menu */
1649 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1650 break;
1653 case WM_MENUSELECT:
1655 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1656 if ( fuFlags & MF_SYSMENU )
1657 goto NEXTHOOK;
1659 /* Save the state for whether this is a server owned popup menu */
1660 else if ( fuFlags & MF_POPUP )
1661 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1663 break;
1666 case WM_DRAWITEM:
1668 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1669 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1670 goto NEXTHOOK; /* Not a menu message */
1672 break;
1675 default:
1676 goto NEXTHOOK;
1679 /* If the message was for the server dispatch it accordingly */
1680 if ( pOleMenuDescriptor->bIsServerItem )
1682 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1683 pMsg->message, pMsg->wParam, pMsg->lParam );
1686 NEXTHOOK:
1687 if ( pOleMenuDescriptor )
1688 GlobalUnlock( hOleMenu );
1690 /* Lookup the hook item for the current thread */
1691 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1693 /* This should never fail!! */
1694 WARN("could not retrieve hHook for current thread!\n" );
1695 return 0;
1698 /* Pass on the message to the next hooker */
1699 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1702 /*************************************************************************
1703 * OLEMenu_GetMsgProc
1704 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1705 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1707 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1709 LPMSG pMsg;
1710 HOLEMENU hOleMenu = 0;
1711 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1712 OleMenuHookItem *pHookItem = NULL;
1713 WORD wCode;
1715 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1717 /* Check if we're being asked to process a messages */
1718 if ( HC_ACTION != code )
1719 goto NEXTHOOK;
1721 /* Retrieve the current message being dispatched from lParam */
1722 pMsg = (LPMSG)lParam;
1724 /* Check if the message is destined for a window we are interested in:
1725 * If the window has an OLEMenu property we may need to dispatch
1726 * the menu message to its active objects window instead. */
1728 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1729 if ( !hOleMenu )
1730 goto NEXTHOOK;
1732 /* Process menu messages */
1733 switch( pMsg->message )
1735 case WM_COMMAND:
1737 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1738 if ( wCode )
1739 goto NEXTHOOK; /* Not a menu message */
1740 break;
1742 default:
1743 goto NEXTHOOK;
1746 /* Get the menu descriptor */
1747 pOleMenuDescriptor = GlobalLock( hOleMenu );
1748 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1749 goto NEXTHOOK;
1751 /* If the message was for the server dispatch it accordingly */
1752 if ( pOleMenuDescriptor->bIsServerItem )
1754 /* Change the hWnd in the message to the active objects hWnd.
1755 * The message loop which reads this message will automatically
1756 * dispatch it to the embedded objects window. */
1757 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1760 NEXTHOOK:
1761 if ( pOleMenuDescriptor )
1762 GlobalUnlock( hOleMenu );
1764 /* Lookup the hook item for the current thread */
1765 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1767 /* This should never fail!! */
1768 WARN("could not retrieve hHook for current thread!\n" );
1769 return FALSE;
1772 /* Pass on the message to the next hooker */
1773 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1776 /***********************************************************************
1777 * OleCreateMenuDescriptor [OLE32.@]
1778 * Creates an OLE menu descriptor for OLE to use when dispatching
1779 * menu messages and commands.
1781 * PARAMS:
1782 * hmenuCombined - Handle to the objects combined menu
1783 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1786 HOLEMENU WINAPI OleCreateMenuDescriptor(
1787 HMENU hmenuCombined,
1788 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1790 HOLEMENU hOleMenu;
1791 OleMenuDescriptor *pOleMenuDescriptor;
1792 int i;
1794 if ( !hmenuCombined || !lpMenuWidths )
1795 return 0;
1797 /* Create an OLE menu descriptor */
1798 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1799 sizeof(OleMenuDescriptor) ) ) )
1800 return 0;
1802 pOleMenuDescriptor = GlobalLock( hOleMenu );
1803 if ( !pOleMenuDescriptor )
1804 return 0;
1806 /* Initialize menu group widths and hmenu */
1807 for ( i = 0; i < 6; i++ )
1808 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1810 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1811 pOleMenuDescriptor->bIsServerItem = FALSE;
1812 GlobalUnlock( hOleMenu );
1814 return hOleMenu;
1817 /***********************************************************************
1818 * OleDestroyMenuDescriptor [OLE32.@]
1819 * Destroy the shared menu descriptor
1821 HRESULT WINAPI OleDestroyMenuDescriptor(
1822 HOLEMENU hmenuDescriptor)
1824 if ( hmenuDescriptor )
1825 GlobalFree( hmenuDescriptor );
1826 return S_OK;
1829 /***********************************************************************
1830 * OleSetMenuDescriptor [OLE32.@]
1831 * Installs or removes OLE dispatching code for the containers frame window.
1833 * PARAMS
1834 * hOleMenu Handle to composite menu descriptor
1835 * hwndFrame Handle to containers frame window
1836 * hwndActiveObject Handle to objects in-place activation window
1837 * lpFrame Pointer to IOleInPlaceFrame on containers window
1838 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1840 * RETURNS
1841 * S_OK - menu installed correctly
1842 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1844 * FIXME
1845 * The lpFrame and lpActiveObject parameters are currently ignored
1846 * OLE should install context sensitive help F1 filtering for the app when
1847 * these are non null.
1849 HRESULT WINAPI OleSetMenuDescriptor(
1850 HOLEMENU hOleMenu,
1851 HWND hwndFrame,
1852 HWND hwndActiveObject,
1853 LPOLEINPLACEFRAME lpFrame,
1854 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1856 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1858 /* Check args */
1859 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1860 return E_INVALIDARG;
1862 if ( lpFrame || lpActiveObject )
1864 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1865 hOleMenu,
1866 hwndFrame,
1867 hwndActiveObject,
1868 lpFrame,
1869 lpActiveObject);
1872 /* Set up a message hook to intercept the containers frame window messages.
1873 * The message filter is responsible for dispatching menu messages from the
1874 * shared menu which are intended for the object.
1877 if ( hOleMenu ) /* Want to install dispatching code */
1879 /* If OLEMenu hooks are already installed for this thread, fail
1880 * Note: This effectively means that OleSetMenuDescriptor cannot
1881 * be called twice in succession on the same frame window
1882 * without first calling it with a null hOleMenu to uninstall
1884 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1885 return E_FAIL;
1887 /* Get the menu descriptor */
1888 pOleMenuDescriptor = GlobalLock( hOleMenu );
1889 if ( !pOleMenuDescriptor )
1890 return E_UNEXPECTED;
1892 /* Update the menu descriptor */
1893 pOleMenuDescriptor->hwndFrame = hwndFrame;
1894 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1896 GlobalUnlock( hOleMenu );
1897 pOleMenuDescriptor = NULL;
1899 /* Add a menu descriptor windows property to the frame window */
1900 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1902 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1903 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1904 return E_FAIL;
1906 else /* Want to uninstall dispatching code */
1908 /* Uninstall the hooks */
1909 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1910 return E_FAIL;
1912 /* Remove the menu descriptor property from the frame window */
1913 RemovePropW( hwndFrame, prop_olemenuW );
1916 return S_OK;
1919 /******************************************************************************
1920 * IsAccelerator [OLE32.@]
1921 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1923 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1925 LPACCEL lpAccelTbl;
1926 int i;
1928 if(!lpMsg) return FALSE;
1929 if (!hAccel)
1931 WARN_(accel)("NULL accel handle\n");
1932 return FALSE;
1934 if((lpMsg->message != WM_KEYDOWN &&
1935 lpMsg->message != WM_SYSKEYDOWN &&
1936 lpMsg->message != WM_SYSCHAR &&
1937 lpMsg->message != WM_CHAR)) return FALSE;
1938 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1939 if (NULL == lpAccelTbl)
1941 return FALSE;
1943 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1945 WARN_(accel)("CopyAcceleratorTableW failed\n");
1946 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1947 return FALSE;
1950 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1951 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
1952 hAccel, cAccelEntries,
1953 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1954 for(i = 0; i < cAccelEntries; i++)
1956 if(lpAccelTbl[i].key != lpMsg->wParam)
1957 continue;
1959 if(lpMsg->message == WM_CHAR)
1961 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1963 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
1964 goto found;
1967 else
1969 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1971 INT mask = 0;
1972 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
1973 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1974 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1975 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1976 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1977 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1978 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1980 else
1982 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1984 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1985 { /* ^^ ALT pressed */
1986 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
1987 goto found;
1994 WARN_(accel)("couldn't translate accelerator key\n");
1995 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1996 return FALSE;
1998 found:
1999 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
2000 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2001 return TRUE;
2004 /***********************************************************************
2005 * ReleaseStgMedium [OLE32.@]
2007 void WINAPI ReleaseStgMedium(
2008 STGMEDIUM* pmedium)
2010 if (!pmedium) return;
2012 switch (pmedium->tymed)
2014 case TYMED_HGLOBAL:
2016 if ( (pmedium->pUnkForRelease==0) &&
2017 (pmedium->u.hGlobal!=0) )
2018 GlobalFree(pmedium->u.hGlobal);
2019 break;
2021 case TYMED_FILE:
2023 if (pmedium->u.lpszFileName!=0)
2025 if (pmedium->pUnkForRelease==0)
2027 DeleteFileW(pmedium->u.lpszFileName);
2030 CoTaskMemFree(pmedium->u.lpszFileName);
2032 break;
2034 case TYMED_ISTREAM:
2036 if (pmedium->u.pstm!=0)
2038 IStream_Release(pmedium->u.pstm);
2040 break;
2042 case TYMED_ISTORAGE:
2044 if (pmedium->u.pstg!=0)
2046 IStorage_Release(pmedium->u.pstg);
2048 break;
2050 case TYMED_GDI:
2052 if ( (pmedium->pUnkForRelease==0) &&
2053 (pmedium->u.hBitmap!=0) )
2054 DeleteObject(pmedium->u.hBitmap);
2055 break;
2057 case TYMED_MFPICT:
2059 if ( (pmedium->pUnkForRelease==0) &&
2060 (pmedium->u.hMetaFilePict!=0) )
2062 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
2063 DeleteMetaFile(pMP->hMF);
2064 GlobalUnlock(pmedium->u.hMetaFilePict);
2065 GlobalFree(pmedium->u.hMetaFilePict);
2067 break;
2069 case TYMED_ENHMF:
2071 if ( (pmedium->pUnkForRelease==0) &&
2072 (pmedium->u.hEnhMetaFile!=0) )
2074 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
2076 break;
2078 case TYMED_NULL:
2079 default:
2080 break;
2082 pmedium->tymed=TYMED_NULL;
2085 * After cleaning up, the unknown is released
2087 if (pmedium->pUnkForRelease!=0)
2089 IUnknown_Release(pmedium->pUnkForRelease);
2090 pmedium->pUnkForRelease = 0;
2094 /***
2095 * OLEDD_Initialize()
2097 * Initializes the OLE drag and drop data structures.
2099 static void OLEDD_Initialize(void)
2101 WNDCLASSW wndClass;
2103 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2104 wndClass.style = CS_GLOBALCLASS;
2105 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
2106 wndClass.cbClsExtra = 0;
2107 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
2108 wndClass.hCursor = 0;
2109 wndClass.hbrBackground = 0;
2110 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
2112 RegisterClassW (&wndClass);
2115 /***
2116 * OLEDD_DragTrackerWindowProc()
2118 * This method is the WindowProcedure of the drag n drop tracking
2119 * window. During a drag n Drop operation, an invisible window is created
2120 * to receive the user input and act upon it. This procedure is in charge
2121 * of this behavior.
2124 #define DRAG_TIMER_ID 1
2126 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
2127 HWND hwnd,
2128 UINT uMsg,
2129 WPARAM wParam,
2130 LPARAM lParam)
2132 switch (uMsg)
2134 case WM_CREATE:
2136 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
2138 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
2139 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
2141 break;
2143 case WM_TIMER:
2144 case WM_MOUSEMOVE:
2145 case WM_LBUTTONUP:
2146 case WM_MBUTTONUP:
2147 case WM_RBUTTONUP:
2148 case WM_LBUTTONDOWN:
2149 case WM_MBUTTONDOWN:
2150 case WM_RBUTTONDOWN:
2152 TrackerWindowInfo *trackerInfo = (TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0);
2153 if (trackerInfo->trackingDone) break;
2154 OLEDD_TrackStateChange(trackerInfo);
2155 break;
2157 case WM_DESTROY:
2159 KillTimer(hwnd, DRAG_TIMER_ID);
2160 break;
2165 * This is a window proc after all. Let's call the default.
2167 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2170 static void drag_enter( TrackerWindowInfo *info, HWND new_target )
2172 HRESULT hr;
2174 info->curTargetHWND = new_target;
2176 while (new_target && !is_droptarget( new_target ))
2177 new_target = GetParent( new_target );
2179 info->curDragTarget = get_droptarget_pointer( new_target );
2181 if (info->curDragTarget)
2183 *info->pdwEffect = info->dwOKEffect;
2184 hr = IDropTarget_DragEnter( info->curDragTarget, info->dataObject,
2185 info->dwKeyState, info->curMousePos,
2186 info->pdwEffect );
2187 *info->pdwEffect &= info->dwOKEffect;
2189 /* failed DragEnter() means invalid target */
2190 if (hr != S_OK)
2192 IDropTarget_Release( info->curDragTarget );
2193 info->curDragTarget = NULL;
2194 info->curTargetHWND = NULL;
2199 static void drag_end( TrackerWindowInfo *info )
2201 HRESULT hr;
2203 info->trackingDone = TRUE;
2204 ReleaseCapture();
2206 if (info->curDragTarget)
2208 if (info->returnValue == DRAGDROP_S_DROP &&
2209 *info->pdwEffect != DROPEFFECT_NONE)
2211 *info->pdwEffect = info->dwOKEffect;
2212 hr = IDropTarget_Drop( info->curDragTarget, info->dataObject, info->dwKeyState,
2213 info->curMousePos, info->pdwEffect );
2214 *info->pdwEffect &= info->dwOKEffect;
2216 if (FAILED( hr ))
2217 info->returnValue = hr;
2219 else
2221 IDropTarget_DragLeave( info->curDragTarget );
2222 *info->pdwEffect = DROPEFFECT_NONE;
2224 IDropTarget_Release( info->curDragTarget );
2225 info->curDragTarget = NULL;
2227 else
2228 *info->pdwEffect = DROPEFFECT_NONE;
2231 static HRESULT give_feedback( TrackerWindowInfo *info )
2233 HRESULT hr;
2234 int res;
2235 HCURSOR cur;
2237 if (info->curDragTarget == NULL)
2238 *info->pdwEffect = DROPEFFECT_NONE;
2240 hr = IDropSource_GiveFeedback( info->dropSource, *info->pdwEffect );
2242 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2244 if (*info->pdwEffect & DROPEFFECT_MOVE)
2245 res = CURSOR_MOVE;
2246 else if (*info->pdwEffect & DROPEFFECT_COPY)
2247 res = CURSOR_COPY;
2248 else if (*info->pdwEffect & DROPEFFECT_LINK)
2249 res = CURSOR_LINK;
2250 else
2251 res = CURSOR_NODROP;
2253 cur = LoadCursorW( hProxyDll, MAKEINTRESOURCEW( res ) );
2254 SetCursor( cur );
2257 return hr;
2260 /***
2261 * OLEDD_TrackStateChange()
2263 * This method is invoked while a drag and drop operation is in effect.
2265 * params:
2266 * trackerInfo - Pointer to the structure identifying the
2267 * drag & drop operation that is currently
2268 * active.
2270 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2272 HWND hwndNewTarget = 0;
2273 POINT pt;
2276 * This method may be called from QueryContinueDrag again,
2277 * (i.e. by running message loop) so avoid recursive call chain.
2279 if (trackerInfo->inTrackCall) return;
2280 trackerInfo->inTrackCall = TRUE;
2283 * Get the handle of the window under the mouse
2285 pt.x = trackerInfo->curMousePos.x;
2286 pt.y = trackerInfo->curMousePos.y;
2287 hwndNewTarget = WindowFromPoint(pt);
2289 trackerInfo->returnValue = IDropSource_QueryContinueDrag(trackerInfo->dropSource,
2290 trackerInfo->escPressed,
2291 trackerInfo->dwKeyState);
2293 if (trackerInfo->curTargetHWND != hwndNewTarget &&
2294 (trackerInfo->returnValue == S_OK ||
2295 trackerInfo->returnValue == DRAGDROP_S_DROP))
2297 if (trackerInfo->curDragTarget)
2299 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2300 IDropTarget_Release(trackerInfo->curDragTarget);
2301 trackerInfo->curDragTarget = NULL;
2302 trackerInfo->curTargetHWND = NULL;
2305 if (hwndNewTarget)
2306 drag_enter( trackerInfo, hwndNewTarget );
2308 give_feedback( trackerInfo );
2312 if (trackerInfo->returnValue == S_OK)
2314 if (trackerInfo->curDragTarget)
2316 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2317 IDropTarget_DragOver(trackerInfo->curDragTarget,
2318 trackerInfo->dwKeyState,
2319 trackerInfo->curMousePos,
2320 trackerInfo->pdwEffect);
2321 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
2323 give_feedback( trackerInfo );
2325 else
2326 drag_end( trackerInfo );
2328 trackerInfo->inTrackCall = FALSE;
2331 /***
2332 * OLEDD_GetButtonState()
2334 * This method will use the current state of the keyboard to build
2335 * a button state mask equivalent to the one passed in the
2336 * WM_MOUSEMOVE wParam.
2338 static DWORD OLEDD_GetButtonState(void)
2340 BYTE keyboardState[256];
2341 DWORD keyMask = 0;
2343 GetKeyboardState(keyboardState);
2345 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2346 keyMask |= MK_SHIFT;
2348 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2349 keyMask |= MK_CONTROL;
2351 if ( (keyboardState[VK_MENU] & 0x80) !=0)
2352 keyMask |= MK_ALT;
2354 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2355 keyMask |= MK_LBUTTON;
2357 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2358 keyMask |= MK_RBUTTON;
2360 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2361 keyMask |= MK_MBUTTON;
2363 return keyMask;
2366 /***
2367 * OLEDD_GetButtonState()
2369 * This method will read the default value of the registry key in
2370 * parameter and extract a DWORD value from it. The registry key value
2371 * can be in a string key or a DWORD key.
2373 * params:
2374 * regKey - Key to read the default value from
2375 * pdwValue - Pointer to the location where the DWORD
2376 * value is returned. This value is not modified
2377 * if the value is not found.
2380 static void OLEUTL_ReadRegistryDWORDValue(
2381 HKEY regKey,
2382 DWORD* pdwValue)
2384 WCHAR buffer[20];
2385 DWORD cbData = sizeof(buffer);
2386 DWORD dwKeyType;
2387 LONG lres;
2389 lres = RegQueryValueExW(regKey, L"", NULL, &dwKeyType, (BYTE *)buffer, &cbData);
2391 if (lres==ERROR_SUCCESS)
2393 switch (dwKeyType)
2395 case REG_DWORD:
2396 *pdwValue = *(DWORD*)buffer;
2397 break;
2398 case REG_EXPAND_SZ:
2399 case REG_MULTI_SZ:
2400 case REG_SZ:
2401 *pdwValue = wcstoul(buffer, NULL, 10);
2402 break;
2407 /******************************************************************************
2408 * OleDraw (OLE32.@)
2410 * The operation of this function is documented literally in the WinAPI
2411 * documentation to involve a QueryInterface for the IViewObject interface,
2412 * followed by a call to IViewObject::Draw.
2414 HRESULT WINAPI OleDraw(
2415 IUnknown *pUnk,
2416 DWORD dwAspect,
2417 HDC hdcDraw,
2418 LPCRECT rect)
2420 HRESULT hres;
2421 IViewObject *viewobject;
2423 if (!pUnk) return E_INVALIDARG;
2425 hres = IUnknown_QueryInterface(pUnk,
2426 &IID_IViewObject,
2427 (void**)&viewobject);
2428 if (SUCCEEDED(hres))
2430 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, (RECTL*)rect, 0, 0, 0);
2431 IViewObject_Release(viewobject);
2432 return hres;
2434 else
2435 return DV_E_NOIVIEWOBJECT;
2438 /***********************************************************************
2439 * OleTranslateAccelerator [OLE32.@]
2441 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2442 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2444 WORD wID;
2446 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2448 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2449 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2451 return S_FALSE;
2454 /******************************************************************************
2455 * OleCreate [OLE32.@]
2458 HRESULT WINAPI OleCreate(
2459 REFCLSID rclsid,
2460 REFIID riid,
2461 DWORD renderopt,
2462 LPFORMATETC pFormatEtc,
2463 LPOLECLIENTSITE pClientSite,
2464 LPSTORAGE pStg,
2465 LPVOID* ppvObj)
2467 HRESULT hres;
2468 IUnknown * pUnk = NULL;
2469 IOleObject *pOleObject = NULL;
2471 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2472 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2474 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2476 if (SUCCEEDED(hres))
2477 hres = IStorage_SetClass(pStg, rclsid);
2479 if (pClientSite && SUCCEEDED(hres))
2481 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2482 if (SUCCEEDED(hres))
2484 DWORD dwStatus;
2485 IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2489 if (SUCCEEDED(hres))
2491 IPersistStorage * pPS;
2492 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2494 TRACE("trying to set stg %p\n", pStg);
2495 hres = IPersistStorage_InitNew(pPS, pStg);
2496 TRACE("-- result 0x%08x\n", hres);
2497 IPersistStorage_Release(pPS);
2501 if (pClientSite && SUCCEEDED(hres))
2503 TRACE("trying to set clientsite %p\n", pClientSite);
2504 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2505 TRACE("-- result 0x%08x\n", hres);
2508 if (pOleObject)
2509 IOleObject_Release(pOleObject);
2511 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2512 SUCCEEDED(hres))
2514 hres = OleRun(pUnk);
2515 if (SUCCEEDED(hres))
2517 IOleCache *pOleCache;
2519 if (SUCCEEDED(IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache)))
2521 DWORD dwConnection;
2522 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2523 FORMATETC pfe;
2524 pfe.cfFormat = 0;
2525 pfe.ptd = NULL;
2526 pfe.dwAspect = DVASPECT_CONTENT;
2527 pfe.lindex = -1;
2528 pfe.tymed = TYMED_NULL;
2529 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2531 else
2532 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2533 IOleCache_Release(pOleCache);
2538 if (FAILED(hres) && pUnk)
2540 IUnknown_Release(pUnk);
2541 pUnk = NULL;
2544 *ppvObj = pUnk;
2546 TRACE("-- %p\n", pUnk);
2547 return hres;
2550 /******************************************************************************
2551 * OleGetAutoConvert [OLE32.@]
2553 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2555 HKEY hkey = NULL;
2556 WCHAR buf[CHARS_IN_GUID];
2557 LONG len;
2558 HRESULT res = S_OK;
2560 res = COM_OpenKeyForCLSID(clsidOld, L"AutoConvertTo", KEY_READ, &hkey);
2561 if (FAILED(res))
2562 goto done;
2564 len = sizeof(buf);
2565 if (RegQueryValueW(hkey, NULL, buf, &len))
2567 res = REGDB_E_KEYMISSING;
2568 goto done;
2570 res = CLSIDFromString(buf, pClsidNew);
2571 done:
2572 if (hkey) RegCloseKey(hkey);
2573 return res;
2576 /******************************************************************************
2577 * OleSetAutoConvert [OLE32.@]
2579 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2581 HKEY hkey = NULL;
2582 WCHAR szClsidNew[CHARS_IN_GUID];
2583 HRESULT res = S_OK;
2585 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2587 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2588 if (FAILED(res))
2589 goto done;
2590 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2591 if (RegSetValueW(hkey, L"AutoConvertTo", REG_SZ, szClsidNew, (lstrlenW(szClsidNew)+1) * sizeof(WCHAR)))
2593 res = REGDB_E_WRITEREGDB;
2594 goto done;
2597 done:
2598 if (hkey) RegCloseKey(hkey);
2599 return res;
2602 /******************************************************************************
2603 * OleDoAutoConvert [OLE32.@]
2605 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2607 WCHAR *user_type_old, *user_type_new;
2608 CLIPFORMAT cf;
2609 STATSTG stat;
2610 CLSID clsid;
2611 HRESULT hr;
2613 TRACE("(%p, %p)\n", pStg, pClsidNew);
2615 *pClsidNew = CLSID_NULL;
2616 if(!pStg)
2617 return E_INVALIDARG;
2618 hr = IStorage_Stat(pStg, &stat, STATFLAG_NONAME);
2619 if(FAILED(hr))
2620 return hr;
2622 *pClsidNew = stat.clsid;
2623 hr = OleGetAutoConvert(&stat.clsid, &clsid);
2624 if(FAILED(hr))
2625 return hr;
2627 hr = IStorage_SetClass(pStg, &clsid);
2628 if(FAILED(hr))
2629 return hr;
2631 hr = ReadFmtUserTypeStg(pStg, &cf, &user_type_old);
2632 if(FAILED(hr)) {
2633 cf = 0;
2634 user_type_new = NULL;
2637 hr = OleRegGetUserType(&clsid, USERCLASSTYPE_FULL, &user_type_new);
2638 if(FAILED(hr))
2639 user_type_new = NULL;
2641 hr = WriteFmtUserTypeStg(pStg, cf, user_type_new);
2642 CoTaskMemFree(user_type_new);
2643 if(FAILED(hr))
2645 CoTaskMemFree(user_type_old);
2646 IStorage_SetClass(pStg, &stat.clsid);
2647 return hr;
2650 hr = SetConvertStg(pStg, TRUE);
2651 if(FAILED(hr))
2653 WriteFmtUserTypeStg(pStg, cf, user_type_old);
2654 IStorage_SetClass(pStg, &stat.clsid);
2656 else
2657 *pClsidNew = clsid;
2658 CoTaskMemFree(user_type_old);
2659 return hr;
2662 /******************************************************************************
2663 * OleIsRunning [OLE32.@]
2665 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2667 IRunnableObject *pRunnable;
2668 HRESULT hr;
2669 BOOL running;
2671 TRACE("(%p)\n", object);
2673 if (!object) return FALSE;
2675 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2676 if (FAILED(hr))
2677 return TRUE;
2678 running = IRunnableObject_IsRunning(pRunnable);
2679 IRunnableObject_Release(pRunnable);
2680 return running;
2683 /***********************************************************************
2684 * OleNoteObjectVisible [OLE32.@]
2686 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2688 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2689 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2692 /***********************************************************************
2693 * PropSysAllocString [OLE32.@]
2694 * NOTES
2695 * Forward to oleaut32.
2697 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2699 return SysAllocString(str);
2702 /***********************************************************************
2703 * PropSysFreeString [OLE32.@]
2704 * NOTES
2705 * Forward to oleaut32.
2707 void WINAPI PropSysFreeString(LPOLESTR str)
2709 SysFreeString(str);