makefiles: Explicitly create destination dirs when installing symlinks.
[wine/zf.git] / dlls / ole32 / ole2.c
blob4464ba0714475087c48a255132d9a5468c01338e
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[] =
105 {'W','i','n','e','D','r','a','g','D','r','o','p','T','r','a','c','k','e','r','3','2',0};
108 * Name of menu descriptor property.
110 static const WCHAR prop_olemenuW[] =
111 {'P','R','O','P','_','O','L','E','M','e','n','u','D','e','s','c','r','i','p','t','o','r',0};
113 /* property to store IDropTarget pointer */
114 static const WCHAR prop_oledroptarget[] =
115 {'O','l','e','D','r','o','p','T','a','r','g','e','t','I','n','t','e','r','f','a','c','e',0};
117 /* property to store Marshalled IDropTarget pointer */
118 static const WCHAR prop_marshalleddroptarget[] =
119 {'W','i','n','e','M','a','r','s','h','a','l','l','e','d','D','r','o','p','T','a','r','g','e','t',0};
121 static const WCHAR emptyW[] = { 0 };
123 /******************************************************************************
124 * These are the prototypes of miscellaneous utility methods
126 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
128 /******************************************************************************
129 * These are the prototypes of the utility methods used to manage a shared menu
131 static void OLEMenu_Initialize(void);
132 static void OLEMenu_UnInitialize(void);
133 static BOOL OLEMenu_InstallHooks( DWORD tid );
134 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
135 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
136 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
137 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
138 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
139 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
141 /******************************************************************************
142 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
144 extern void OLEClipbrd_UnInitialize(void);
145 extern void OLEClipbrd_Initialize(void);
147 /******************************************************************************
148 * These are the prototypes of the utility methods used for OLE Drag n Drop
150 static void OLEDD_Initialize(void);
151 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
152 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo);
153 static DWORD OLEDD_GetButtonState(void);
155 /******************************************************************************
156 * OleBuildVersion [OLE32.@]
158 DWORD WINAPI OleBuildVersion(void)
160 TRACE("Returning version %d, build %d.\n", rmm, rup);
161 return (rmm<<16)+rup;
164 /***********************************************************************
165 * OleInitialize (OLE32.@)
167 HRESULT WINAPI DECLSPEC_HOTPATCH OleInitialize(LPVOID reserved)
169 HRESULT hr;
171 TRACE("(%p)\n", reserved);
174 * The first duty of the OleInitialize is to initialize the COM libraries.
176 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
179 * If the CoInitializeEx call failed, the OLE libraries can't be
180 * initialized.
182 if (FAILED(hr))
183 return hr;
185 if (!COM_CurrentInfo()->ole_inits)
186 hr = S_OK;
187 else
188 hr = S_FALSE;
191 * Then, it has to initialize the OLE specific modules.
192 * This includes:
193 * Clipboard
194 * Drag and Drop
195 * Object linking and Embedding
196 * In-place activation
198 if (!COM_CurrentInfo()->ole_inits++ &&
199 InterlockedIncrement(&OLE_moduleLockCount) == 1)
202 * Initialize the libraries.
204 TRACE("() - Initializing the OLE libraries\n");
207 * OLE Clipboard
209 OLEClipbrd_Initialize();
212 * Drag and Drop
214 OLEDD_Initialize();
217 * OLE shared menu
219 OLEMenu_Initialize();
222 return hr;
225 /******************************************************************************
226 * OleUninitialize [OLE32.@]
228 void WINAPI DECLSPEC_HOTPATCH OleUninitialize(void)
230 TRACE("()\n");
232 if (COM_CurrentInfo()->ole_inits == 0)
234 WARN("ole_inits is already 0\n");
235 return ;
238 * If we hit the bottom of the lock stack, free the libraries.
240 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
243 * Actually free the libraries.
245 TRACE("() - Freeing the last reference count\n");
248 * OLE Clipboard
250 OLEClipbrd_UnInitialize();
253 * OLE shared menu
255 OLEMenu_UnInitialize();
259 * Then, uninitialize the COM libraries.
261 CoUninitialize();
264 /******************************************************************************
265 * OleInitializeWOW [OLE32.@]
267 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
268 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
269 return 0;
272 /*************************************************************
273 * get_droptarget_handle
275 * Retrieve a handle to the map containing the marshalled IDropTarget.
276 * This handle belongs to the process that called RegisterDragDrop.
277 * See get_droptarget_local_handle().
279 static inline HANDLE get_droptarget_handle(HWND hwnd)
281 return GetPropW(hwnd, prop_marshalleddroptarget);
284 /*************************************************************
285 * is_droptarget
287 * Is the window a droptarget.
289 static inline BOOL is_droptarget(HWND hwnd)
291 return get_droptarget_handle(hwnd) != 0;
294 /*************************************************************
295 * get_droptarget_local_handle
297 * Retrieve a handle to the map containing the marshalled IDropTarget.
298 * The handle should be closed when finished with.
300 static HANDLE get_droptarget_local_handle(HWND hwnd)
302 HANDLE handle, local_handle = 0;
304 handle = get_droptarget_handle(hwnd);
306 if(handle)
308 DWORD pid;
309 HANDLE process;
311 GetWindowThreadProcessId(hwnd, &pid);
312 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
313 if(process)
315 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
316 CloseHandle(process);
319 return local_handle;
322 /***********************************************************************
323 * create_map_from_stream
325 * Helper for RegisterDragDrop. Creates a file mapping object
326 * with the contents of the provided stream. The stream must
327 * be a global memory backed stream.
329 static HRESULT create_map_from_stream(IStream *stream, HANDLE *map)
331 HGLOBAL hmem;
332 DWORD size;
333 HRESULT hr;
334 void *data;
336 hr = GetHGlobalFromStream(stream, &hmem);
337 if(FAILED(hr)) return hr;
339 size = GlobalSize(hmem);
340 *map = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, NULL);
341 if(!*map) return E_OUTOFMEMORY;
343 data = MapViewOfFile(*map, FILE_MAP_WRITE, 0, 0, size);
344 memcpy(data, GlobalLock(hmem), size);
345 GlobalUnlock(hmem);
346 UnmapViewOfFile(data);
347 return S_OK;
350 /***********************************************************************
351 * create_stream_from_map
353 * Creates a stream from the provided map.
355 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
357 HRESULT hr = E_OUTOFMEMORY;
358 HGLOBAL hmem;
359 void *data;
360 MEMORY_BASIC_INFORMATION info;
362 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
363 if(!data) return hr;
365 VirtualQuery(data, &info, sizeof(info));
366 TRACE("size %d\n", (int)info.RegionSize);
368 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
369 if(hmem)
371 memcpy(GlobalLock(hmem), data, info.RegionSize);
372 GlobalUnlock(hmem);
373 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
375 UnmapViewOfFile(data);
376 return hr;
379 /* This is to work around apps which break COM rules by not implementing
380 * IDropTarget::QueryInterface(). Windows doesn't expose this because it
381 * doesn't call CoMarshallInterface() in RegisterDragDrop().
382 * The wrapper is only used internally, and only exists for the life of
383 * the marshal. We don't want to hold a ref on the app provided target
384 * as some apps destroy this prior to CoUninitialize without calling
385 * RevokeDragDrop. The only (long-term) ref is held by the window prop. */
386 typedef struct {
387 IDropTarget IDropTarget_iface;
388 HWND hwnd;
389 LONG refs;
390 } DropTargetWrapper;
392 static inline DropTargetWrapper* impl_from_IDropTarget(IDropTarget* iface)
394 return CONTAINING_RECORD(iface, DropTargetWrapper, IDropTarget_iface);
397 static HRESULT WINAPI DropTargetWrapper_QueryInterface(IDropTarget* iface,
398 REFIID riid,
399 void** ppvObject)
401 DropTargetWrapper* This = impl_from_IDropTarget(iface);
402 if (IsEqualIID(riid, &IID_IUnknown) ||
403 IsEqualIID(riid, &IID_IDropTarget))
405 IDropTarget_AddRef(&This->IDropTarget_iface);
406 *ppvObject = &This->IDropTarget_iface;
407 return S_OK;
409 *ppvObject = NULL;
410 return E_NOINTERFACE;
413 static ULONG WINAPI DropTargetWrapper_AddRef(IDropTarget* iface)
415 DropTargetWrapper* This = impl_from_IDropTarget(iface);
416 return InterlockedIncrement(&This->refs);
419 static ULONG WINAPI DropTargetWrapper_Release(IDropTarget* iface)
421 DropTargetWrapper* This = impl_from_IDropTarget(iface);
422 ULONG refs = InterlockedDecrement(&This->refs);
423 if (!refs) HeapFree(GetProcessHeap(), 0, This);
424 return refs;
427 static inline HRESULT get_target_from_wrapper( IDropTarget *wrapper, IDropTarget **target )
429 DropTargetWrapper* This = impl_from_IDropTarget( wrapper );
430 *target = GetPropW( This->hwnd, prop_oledroptarget );
431 if (!*target) return DRAGDROP_E_NOTREGISTERED;
432 IDropTarget_AddRef( *target );
433 return S_OK;
436 static HRESULT WINAPI DropTargetWrapper_DragEnter(IDropTarget* iface,
437 IDataObject* pDataObj,
438 DWORD grfKeyState,
439 POINTL pt,
440 DWORD* pdwEffect)
442 IDropTarget *target;
443 HRESULT r = get_target_from_wrapper( iface, &target );
445 if (SUCCEEDED( r ))
447 r = IDropTarget_DragEnter( target, pDataObj, grfKeyState, pt, pdwEffect );
448 IDropTarget_Release( target );
450 return r;
453 static HRESULT WINAPI DropTargetWrapper_DragOver(IDropTarget* iface,
454 DWORD grfKeyState,
455 POINTL pt,
456 DWORD* pdwEffect)
458 IDropTarget *target;
459 HRESULT r = get_target_from_wrapper( iface, &target );
461 if (SUCCEEDED( r ))
463 r = IDropTarget_DragOver( target, grfKeyState, pt, pdwEffect );
464 IDropTarget_Release( target );
466 return r;
469 static HRESULT WINAPI DropTargetWrapper_DragLeave(IDropTarget* iface)
471 IDropTarget *target;
472 HRESULT r = get_target_from_wrapper( iface, &target );
474 if (SUCCEEDED( r ))
476 r = IDropTarget_DragLeave( target );
477 IDropTarget_Release( target );
479 return r;
482 static HRESULT WINAPI DropTargetWrapper_Drop(IDropTarget* iface,
483 IDataObject* pDataObj,
484 DWORD grfKeyState,
485 POINTL pt,
486 DWORD* pdwEffect)
488 IDropTarget *target;
489 HRESULT r = get_target_from_wrapper( iface, &target );
491 if (SUCCEEDED( r ))
493 r = IDropTarget_Drop( target, pDataObj, grfKeyState, pt, pdwEffect );
494 IDropTarget_Release( target );
496 return r;
499 static const IDropTargetVtbl DropTargetWrapperVTbl =
501 DropTargetWrapper_QueryInterface,
502 DropTargetWrapper_AddRef,
503 DropTargetWrapper_Release,
504 DropTargetWrapper_DragEnter,
505 DropTargetWrapper_DragOver,
506 DropTargetWrapper_DragLeave,
507 DropTargetWrapper_Drop
510 static IDropTarget* WrapDropTarget( HWND hwnd )
512 DropTargetWrapper* This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
514 if (This)
516 This->IDropTarget_iface.lpVtbl = &DropTargetWrapperVTbl;
517 This->hwnd = hwnd;
518 This->refs = 1;
520 return &This->IDropTarget_iface;
523 /***********************************************************************
524 * get_droptarget_pointer
526 * Retrieves the marshalled IDropTarget from the window.
528 static IDropTarget* get_droptarget_pointer(HWND hwnd)
530 IDropTarget *droptarget = NULL;
531 HANDLE map;
532 IStream *stream;
534 map = get_droptarget_local_handle(hwnd);
535 if(!map) return NULL;
537 if(SUCCEEDED(create_stream_from_map(map, &stream)))
539 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
540 IStream_Release(stream);
542 CloseHandle(map);
543 return droptarget;
546 /***********************************************************************
547 * RegisterDragDrop (OLE32.@)
549 HRESULT WINAPI RegisterDragDrop(HWND hwnd, LPDROPTARGET pDropTarget)
551 DWORD pid = 0;
552 HRESULT hr;
553 IStream *stream;
554 HANDLE map;
555 IDropTarget *wrapper;
557 TRACE("(%p,%p)\n", hwnd, pDropTarget);
559 if (!COM_CurrentApt())
561 ERR("COM not initialized\n");
562 return E_OUTOFMEMORY;
565 if (!pDropTarget)
566 return E_INVALIDARG;
568 if (!IsWindow(hwnd))
570 ERR("invalid hwnd %p\n", hwnd);
571 return DRAGDROP_E_INVALIDHWND;
574 /* block register for other processes windows */
575 GetWindowThreadProcessId(hwnd, &pid);
576 if (pid != GetCurrentProcessId())
578 FIXME("register for another process windows is disabled\n");
579 return DRAGDROP_E_INVALIDHWND;
582 /* check if the window is already registered */
583 if (is_droptarget(hwnd))
584 return DRAGDROP_E_ALREADYREGISTERED;
587 * Marshal the drop target pointer into a shared memory map and
588 * store the map's handle in a Wine specific window prop. We also
589 * store the drop target pointer itself in the
590 * "OleDropTargetInterface" prop for compatibility with Windows.
593 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
594 if(FAILED(hr)) return hr;
596 /* IDropTarget::QueryInterface() shouldn't be called, some (broken) apps depend on this. */
597 wrapper = WrapDropTarget( hwnd );
598 if(!wrapper)
600 IStream_Release(stream);
601 return E_OUTOFMEMORY;
603 hr = CoMarshalInterface(stream, &IID_IDropTarget, (IUnknown*)wrapper, MSHCTX_LOCAL, NULL, MSHLFLAGS_TABLESTRONG);
604 IDropTarget_Release(wrapper);
606 if(SUCCEEDED(hr))
608 hr = create_map_from_stream(stream, &map);
609 if(SUCCEEDED(hr))
611 IDropTarget_AddRef(pDropTarget);
612 SetPropW(hwnd, prop_oledroptarget, pDropTarget);
613 SetPropW(hwnd, prop_marshalleddroptarget, map);
615 else
617 LARGE_INTEGER zero;
618 zero.QuadPart = 0;
619 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
620 CoReleaseMarshalData(stream);
623 IStream_Release(stream);
625 return hr;
628 /***********************************************************************
629 * RevokeDragDrop (OLE32.@)
631 HRESULT WINAPI RevokeDragDrop(HWND hwnd)
633 HANDLE map;
634 IStream *stream;
635 IDropTarget *drop_target;
636 HRESULT hr;
638 TRACE("(%p)\n", hwnd);
640 if (!IsWindow(hwnd))
642 ERR("invalid hwnd %p\n", hwnd);
643 return DRAGDROP_E_INVALIDHWND;
646 /* no registration data */
647 if (!(map = get_droptarget_handle(hwnd)))
648 return DRAGDROP_E_NOTREGISTERED;
650 drop_target = GetPropW(hwnd, prop_oledroptarget);
651 if(drop_target) IDropTarget_Release(drop_target);
653 RemovePropW(hwnd, prop_oledroptarget);
654 RemovePropW(hwnd, prop_marshalleddroptarget);
656 hr = create_stream_from_map(map, &stream);
657 if(SUCCEEDED(hr))
659 CoReleaseMarshalData(stream);
660 IStream_Release(stream);
662 CloseHandle(map);
664 return hr;
667 /***********************************************************************
668 * OleRegGetUserType (OLE32.@)
670 HRESULT WINAPI OleRegGetUserType(REFCLSID clsid, DWORD form, LPOLESTR *usertype)
672 static const WCHAR auxusertypeW[] = {'A','u','x','U','s','e','r','T','y','p','e','\\','%','d',0};
673 DWORD valuetype, valuelen;
674 WCHAR auxkeynameW[16];
675 HKEY usertypekey;
676 HRESULT hres;
677 LONG ret;
679 TRACE("(%s, %u, %p)\n", debugstr_guid(clsid), form, usertype);
681 if (!usertype)
682 return E_INVALIDARG;
684 *usertype = NULL;
686 /* Return immediately if it's not registered. */
687 hres = COM_OpenKeyForCLSID(clsid, NULL, KEY_READ, &usertypekey);
688 if (FAILED(hres))
689 return hres;
691 valuelen = 0;
693 /* Try additional types if requested. If they don't exist fall back to USERCLASSTYPE_FULL. */
694 if (form != USERCLASSTYPE_FULL)
696 HKEY auxkey;
698 swprintf(auxkeynameW, ARRAY_SIZE(auxkeynameW), auxusertypeW, form);
699 if (COM_OpenKeyForCLSID(clsid, auxkeynameW, KEY_READ, &auxkey) == S_OK)
701 if (!RegQueryValueExW(auxkey, emptyW, NULL, &valuetype, NULL, &valuelen) && valuelen)
703 RegCloseKey(usertypekey);
704 usertypekey = auxkey;
706 else
707 RegCloseKey(auxkey);
711 valuelen = 0;
712 if (RegQueryValueExW(usertypekey, emptyW, NULL, &valuetype, NULL, &valuelen))
714 RegCloseKey(usertypekey);
715 return REGDB_E_READREGDB;
718 *usertype = CoTaskMemAlloc(valuelen);
719 if (!*usertype)
721 RegCloseKey(usertypekey);
722 return E_OUTOFMEMORY;
725 ret = RegQueryValueExW(usertypekey,
726 emptyW,
727 NULL,
728 &valuetype,
729 (LPBYTE)*usertype,
730 &valuelen);
731 RegCloseKey(usertypekey);
732 if (ret != ERROR_SUCCESS)
734 CoTaskMemFree(*usertype);
735 *usertype = NULL;
736 return REGDB_E_READREGDB;
739 return S_OK;
742 /***********************************************************************
743 * DoDragDrop [OLE32.@]
745 HRESULT WINAPI DoDragDrop (
746 IDataObject *pDataObject, /* [in] ptr to the data obj */
747 IDropSource* pDropSource, /* [in] ptr to the source obj */
748 DWORD dwOKEffect, /* [in] effects allowed by the source */
749 DWORD *pdwEffect) /* [out] ptr to effects of the source */
751 static const WCHAR trackerW[] = {'T','r','a','c','k','e','r','W','i','n','d','o','w',0};
752 TrackerWindowInfo trackerInfo;
753 HWND hwndTrackWindow;
754 MSG msg;
756 TRACE("(%p, %p, %08x, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
758 if (!pDataObject || !pDropSource || !pdwEffect)
759 return E_INVALIDARG;
762 * Setup the drag n drop tracking window.
765 trackerInfo.dataObject = pDataObject;
766 trackerInfo.dropSource = pDropSource;
767 trackerInfo.dwOKEffect = dwOKEffect;
768 trackerInfo.pdwEffect = pdwEffect;
769 trackerInfo.trackingDone = FALSE;
770 trackerInfo.inTrackCall = FALSE;
771 trackerInfo.escPressed = FALSE;
772 trackerInfo.curTargetHWND = 0;
773 trackerInfo.curDragTarget = 0;
775 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, trackerW,
776 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
777 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
778 &trackerInfo);
780 if (hwndTrackWindow)
783 * Capture the mouse input
785 SetCapture(hwndTrackWindow);
787 msg.message = 0;
790 * Pump messages. All mouse input should go to the capture window.
792 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
794 trackerInfo.curMousePos.x = msg.pt.x;
795 trackerInfo.curMousePos.y = msg.pt.y;
796 trackerInfo.dwKeyState = OLEDD_GetButtonState();
798 if ( (msg.message >= WM_KEYFIRST) &&
799 (msg.message <= WM_KEYLAST) )
802 * When keyboard messages are sent to windows on this thread, we
803 * want to ignore notify the drop source that the state changed.
804 * in the case of the Escape key, we also notify the drop source
805 * we give it a special meaning.
807 if ( (msg.message==WM_KEYDOWN) &&
808 (msg.wParam==VK_ESCAPE) )
810 trackerInfo.escPressed = TRUE;
814 * Notify the drop source.
816 OLEDD_TrackStateChange(&trackerInfo);
818 else
821 * Dispatch the messages only when it's not a keyboard message.
823 DispatchMessageW(&msg);
827 /* re-post the quit message to outer message loop */
828 if (msg.message == WM_QUIT)
829 PostQuitMessage(msg.wParam);
831 * Destroy the temporary window.
833 DestroyWindow(hwndTrackWindow);
835 return trackerInfo.returnValue;
838 return E_FAIL;
841 /***********************************************************************
842 * OleQueryLinkFromData [OLE32.@]
844 HRESULT WINAPI OleQueryLinkFromData(
845 IDataObject* pSrcDataObject)
847 FIXME("(%p),stub!\n", pSrcDataObject);
848 return S_FALSE;
851 /***********************************************************************
852 * OleRegGetMiscStatus [OLE32.@]
854 HRESULT WINAPI OleRegGetMiscStatus(
855 REFCLSID clsid,
856 DWORD dwAspect,
857 DWORD* pdwStatus)
859 static const WCHAR miscstatusW[] = {'M','i','s','c','S','t','a','t','u','s',0};
860 static const WCHAR dfmtW[] = {'%','d',0};
861 WCHAR keyName[16];
862 HKEY miscStatusKey;
863 HKEY aspectKey;
864 LONG result;
865 HRESULT hr;
867 TRACE("(%s, %d, %p)\n", debugstr_guid(clsid), dwAspect, pdwStatus);
869 if (!pdwStatus) return E_INVALIDARG;
871 *pdwStatus = 0;
873 if (actctx_get_miscstatus(clsid, dwAspect, pdwStatus)) return S_OK;
875 hr = COM_OpenKeyForCLSID(clsid, miscstatusW, KEY_READ, &miscStatusKey);
876 if (FAILED(hr))
877 /* missing key is not a failure */
878 return hr == REGDB_E_KEYMISSING ? S_OK : hr;
880 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
883 * Open the key specific to the requested aspect.
885 swprintf(keyName, ARRAY_SIZE(keyName), dfmtW, dwAspect);
887 result = open_classes_key(miscStatusKey, keyName, KEY_READ, &aspectKey);
888 if (result == ERROR_SUCCESS)
890 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
891 RegCloseKey(aspectKey);
894 RegCloseKey(miscStatusKey);
895 return S_OK;
898 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
900 typedef struct
902 IEnumOLEVERB IEnumOLEVERB_iface;
903 LONG ref;
905 HKEY hkeyVerb;
906 ULONG index;
907 } EnumOLEVERB;
909 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
911 return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
914 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
915 IEnumOLEVERB *iface, REFIID riid, void **ppv)
917 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
918 if (IsEqualIID(riid, &IID_IUnknown) ||
919 IsEqualIID(riid, &IID_IEnumOLEVERB))
921 IEnumOLEVERB_AddRef(iface);
922 *ppv = iface;
923 return S_OK;
925 return E_NOINTERFACE;
928 static ULONG WINAPI EnumOLEVERB_AddRef(
929 IEnumOLEVERB *iface)
931 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
932 TRACE("()\n");
933 return InterlockedIncrement(&This->ref);
936 static ULONG WINAPI EnumOLEVERB_Release(
937 IEnumOLEVERB *iface)
939 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
940 LONG refs = InterlockedDecrement(&This->ref);
941 TRACE("()\n");
942 if (!refs)
944 RegCloseKey(This->hkeyVerb);
945 HeapFree(GetProcessHeap(), 0, This);
947 return refs;
950 static HRESULT WINAPI EnumOLEVERB_Next(
951 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
952 ULONG *pceltFetched)
954 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
955 HRESULT hr = S_OK;
957 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
959 if (pceltFetched)
960 *pceltFetched = 0;
962 for (; celt; celt--, rgelt++)
964 WCHAR wszSubKey[20];
965 LONG cbData;
966 LPWSTR pwszOLEVERB;
967 LPWSTR pwszMenuFlags;
968 LPWSTR pwszAttribs;
969 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, ARRAY_SIZE(wszSubKey));
970 if (res == ERROR_NO_MORE_ITEMS)
972 hr = S_FALSE;
973 break;
975 else if (res != ERROR_SUCCESS)
977 ERR("RegEnumKeyW failed with error %d\n", res);
978 hr = REGDB_E_READREGDB;
979 break;
981 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
982 if (res != ERROR_SUCCESS)
984 ERR("RegQueryValueW failed with error %d\n", res);
985 hr = REGDB_E_READREGDB;
986 break;
988 pwszOLEVERB = CoTaskMemAlloc(cbData);
989 if (!pwszOLEVERB)
991 hr = E_OUTOFMEMORY;
992 break;
994 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
995 if (res != ERROR_SUCCESS)
997 ERR("RegQueryValueW failed with error %d\n", res);
998 hr = REGDB_E_READREGDB;
999 CoTaskMemFree(pwszOLEVERB);
1000 break;
1003 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
1004 pwszMenuFlags = wcschr(pwszOLEVERB, ',');
1005 if (!pwszMenuFlags)
1007 hr = OLEOBJ_E_INVALIDVERB;
1008 CoTaskMemFree(pwszOLEVERB);
1009 break;
1011 /* nul terminate the name string and advance to first character */
1012 *pwszMenuFlags = '\0';
1013 pwszMenuFlags++;
1014 pwszAttribs = wcschr(pwszMenuFlags, ',');
1015 if (!pwszAttribs)
1017 hr = OLEOBJ_E_INVALIDVERB;
1018 CoTaskMemFree(pwszOLEVERB);
1019 break;
1021 /* nul terminate the menu string and advance to first character */
1022 *pwszAttribs = '\0';
1023 pwszAttribs++;
1025 /* fill out structure for this verb */
1026 rgelt->lVerb = wcstol(wszSubKey, NULL, 10);
1027 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
1028 rgelt->fuFlags = wcstol(pwszMenuFlags, NULL, 10);
1029 rgelt->grfAttribs = wcstol(pwszAttribs, NULL, 10);
1031 if (pceltFetched)
1032 (*pceltFetched)++;
1033 This->index++;
1035 return hr;
1038 static HRESULT WINAPI EnumOLEVERB_Skip(
1039 IEnumOLEVERB *iface, ULONG celt)
1041 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1043 TRACE("(%d)\n", celt);
1045 This->index += celt;
1046 return S_OK;
1049 static HRESULT WINAPI EnumOLEVERB_Reset(
1050 IEnumOLEVERB *iface)
1052 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1054 TRACE("()\n");
1056 This->index = 0;
1057 return S_OK;
1060 static HRESULT WINAPI EnumOLEVERB_Clone(
1061 IEnumOLEVERB *iface,
1062 IEnumOLEVERB **ppenum)
1064 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1065 HKEY hkeyVerb;
1066 TRACE("(%p)\n", ppenum);
1067 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
1068 return HRESULT_FROM_WIN32(GetLastError());
1069 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
1072 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
1074 EnumOLEVERB_QueryInterface,
1075 EnumOLEVERB_AddRef,
1076 EnumOLEVERB_Release,
1077 EnumOLEVERB_Next,
1078 EnumOLEVERB_Skip,
1079 EnumOLEVERB_Reset,
1080 EnumOLEVERB_Clone
1083 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
1085 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1086 if (!This)
1088 RegCloseKey(hkeyVerb);
1089 return E_OUTOFMEMORY;
1091 This->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERB_VTable;
1092 This->ref = 1;
1093 This->index = index;
1094 This->hkeyVerb = hkeyVerb;
1095 *ppenum = &This->IEnumOLEVERB_iface;
1096 return S_OK;
1099 /***********************************************************************
1100 * OleRegEnumVerbs [OLE32.@]
1102 * Enumerates verbs associated with a class stored in the registry.
1104 * PARAMS
1105 * clsid [I] Class ID to enumerate the verbs for.
1106 * ppenum [O] Enumerator.
1108 * RETURNS
1109 * S_OK: Success.
1110 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1111 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1112 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1113 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1115 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
1117 LONG res;
1118 HKEY hkeyVerb;
1119 DWORD dwSubKeys;
1120 static const WCHAR wszVerb[] = {'V','e','r','b',0};
1122 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
1124 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
1125 if (FAILED(res))
1127 if (res == REGDB_E_CLASSNOTREG)
1128 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
1129 else if (res == REGDB_E_KEYMISSING)
1130 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
1131 else
1132 ERR("failed to open Verbs key for CLSID %s with error %d\n",
1133 debugstr_guid(clsid), res);
1134 return res;
1137 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
1138 NULL, NULL, NULL, NULL, NULL, NULL);
1139 if (res != ERROR_SUCCESS)
1141 ERR("failed to get subkey count with error %d\n", GetLastError());
1142 return REGDB_E_READREGDB;
1145 if (!dwSubKeys)
1147 WARN("class %s has no verbs\n", debugstr_guid(clsid));
1148 RegCloseKey(hkeyVerb);
1149 return OLEOBJ_E_NOVERBS;
1152 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
1155 /******************************************************************************
1156 * OleSetContainedObject [OLE32.@]
1158 HRESULT WINAPI OleSetContainedObject(
1159 LPUNKNOWN pUnknown,
1160 BOOL fContained)
1162 IRunnableObject* runnable = NULL;
1163 HRESULT hres;
1165 TRACE("(%p,%x)\n", pUnknown, fContained);
1167 hres = IUnknown_QueryInterface(pUnknown,
1168 &IID_IRunnableObject,
1169 (void**)&runnable);
1171 if (SUCCEEDED(hres))
1173 hres = IRunnableObject_SetContainedObject(runnable, fContained);
1175 IRunnableObject_Release(runnable);
1177 return hres;
1180 return S_OK;
1183 /******************************************************************************
1184 * OleRun [OLE32.@]
1186 * Set the OLE object to the running state.
1188 * PARAMS
1189 * pUnknown [I] OLE object to run.
1191 * RETURNS
1192 * Success: S_OK.
1193 * Failure: Any HRESULT code.
1195 HRESULT WINAPI DECLSPEC_HOTPATCH OleRun(LPUNKNOWN pUnknown)
1197 IRunnableObject *runable;
1198 HRESULT hres;
1200 TRACE("(%p)\n", pUnknown);
1202 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
1203 if (FAILED(hres))
1204 return S_OK; /* Appears to return no error. */
1206 hres = IRunnableObject_Run(runable, NULL);
1207 IRunnableObject_Release(runable);
1208 return hres;
1211 /******************************************************************************
1212 * OleLoad [OLE32.@]
1214 HRESULT WINAPI OleLoad(
1215 LPSTORAGE pStg,
1216 REFIID riid,
1217 LPOLECLIENTSITE pClientSite,
1218 LPVOID* ppvObj)
1220 IPersistStorage* persistStorage = NULL;
1221 IUnknown* pUnk;
1222 IOleObject* pOleObject = NULL;
1223 STATSTG storageInfo;
1224 HRESULT hres;
1226 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
1228 *ppvObj = NULL;
1231 * TODO, Conversion ... OleDoAutoConvert
1235 * Get the class ID for the object.
1237 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1238 if (FAILED(hres))
1239 return hres;
1242 * Now, try and create the handler for the object
1244 hres = CoCreateInstance(&storageInfo.clsid,
1245 NULL,
1246 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1247 riid,
1248 (void**)&pUnk);
1251 * If that fails, as it will most times, load the default
1252 * OLE handler.
1254 if (FAILED(hres))
1256 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1257 NULL,
1258 riid,
1259 (void**)&pUnk);
1263 * If we couldn't find a handler... this is bad. Abort the whole thing.
1265 if (FAILED(hres))
1266 return hres;
1268 if (pClientSite)
1270 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1271 if (SUCCEEDED(hres))
1273 DWORD dwStatus;
1274 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1279 * Initialize the object with its IPersistStorage interface.
1281 hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (void**)&persistStorage);
1282 if (SUCCEEDED(hres))
1284 hres = IPersistStorage_Load(persistStorage, pStg);
1286 IPersistStorage_Release(persistStorage);
1287 persistStorage = NULL;
1290 if (SUCCEEDED(hres) && pClientSite)
1292 * Inform the new object of its client site.
1294 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1297 * Cleanup interfaces used internally
1299 if (pOleObject)
1300 IOleObject_Release(pOleObject);
1302 if (SUCCEEDED(hres))
1304 IOleLink *pOleLink;
1305 HRESULT hres1;
1306 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1307 if (SUCCEEDED(hres1))
1309 FIXME("handle OLE link\n");
1310 IOleLink_Release(pOleLink);
1314 if (FAILED(hres))
1316 IUnknown_Release(pUnk);
1317 pUnk = NULL;
1320 *ppvObj = pUnk;
1322 return hres;
1325 /***********************************************************************
1326 * OleSave [OLE32.@]
1328 HRESULT WINAPI OleSave(
1329 LPPERSISTSTORAGE pPS,
1330 LPSTORAGE pStg,
1331 BOOL fSameAsLoad)
1333 HRESULT hres;
1334 CLSID objectClass;
1336 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1339 * First, we transfer the class ID (if available)
1341 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1343 if (SUCCEEDED(hres))
1345 WriteClassStg(pStg, &objectClass);
1349 * Then, we ask the object to save itself to the
1350 * storage. If it is successful, we commit the storage.
1352 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1354 if (SUCCEEDED(hres))
1356 IStorage_Commit(pStg,
1357 STGC_DEFAULT);
1360 return hres;
1364 /******************************************************************************
1365 * OleLockRunning [OLE32.@]
1367 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1369 IRunnableObject* runnable = NULL;
1370 HRESULT hres;
1372 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1374 hres = IUnknown_QueryInterface(pUnknown,
1375 &IID_IRunnableObject,
1376 (void**)&runnable);
1378 if (SUCCEEDED(hres))
1380 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1382 IRunnableObject_Release(runnable);
1384 return hres;
1387 return S_OK;
1391 /**************************************************************************
1392 * Internal methods to manage the shared OLE menu in response to the
1393 * OLE***MenuDescriptor API
1396 /***
1397 * OLEMenu_Initialize()
1399 * Initializes the OLEMENU data structures.
1401 static void OLEMenu_Initialize(void)
1405 /***
1406 * OLEMenu_UnInitialize()
1408 * Releases the OLEMENU data structures.
1410 static void OLEMenu_UnInitialize(void)
1414 /*************************************************************************
1415 * OLEMenu_InstallHooks
1416 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1418 * RETURNS: TRUE if message hooks were successfully installed
1419 * FALSE on failure
1421 static BOOL OLEMenu_InstallHooks( DWORD tid )
1423 OleMenuHookItem *pHookItem;
1425 /* Create an entry for the hook table */
1426 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1427 sizeof(OleMenuHookItem)) ) )
1428 return FALSE;
1430 pHookItem->tid = tid;
1431 pHookItem->hHeap = GetProcessHeap();
1432 pHookItem->CallWndProc_hHook = NULL;
1434 /* Install a thread scope message hook for WH_GETMESSAGE */
1435 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1436 0, GetCurrentThreadId() );
1437 if ( !pHookItem->GetMsg_hHook )
1438 goto CLEANUP;
1440 /* Install a thread scope message hook for WH_CALLWNDPROC */
1441 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1442 0, GetCurrentThreadId() );
1443 if ( !pHookItem->CallWndProc_hHook )
1444 goto CLEANUP;
1446 /* Insert the hook table entry */
1447 pHookItem->next = hook_list;
1448 hook_list = pHookItem;
1450 return TRUE;
1452 CLEANUP:
1453 /* Unhook any hooks */
1454 if ( pHookItem->GetMsg_hHook )
1455 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1456 if ( pHookItem->CallWndProc_hHook )
1457 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1458 /* Release the hook table entry */
1459 HeapFree(pHookItem->hHeap, 0, pHookItem );
1461 return FALSE;
1464 /*************************************************************************
1465 * OLEMenu_UnInstallHooks
1466 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1468 * RETURNS: TRUE if message hooks were successfully installed
1469 * FALSE on failure
1471 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1473 OleMenuHookItem *pHookItem = NULL;
1474 OleMenuHookItem **ppHook = &hook_list;
1476 while (*ppHook)
1478 if ((*ppHook)->tid == tid)
1480 pHookItem = *ppHook;
1481 *ppHook = pHookItem->next;
1482 break;
1484 ppHook = &(*ppHook)->next;
1486 if (!pHookItem) return FALSE;
1488 /* Uninstall the hooks installed for this thread */
1489 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1490 goto CLEANUP;
1491 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1492 goto CLEANUP;
1494 /* Release the hook table entry */
1495 HeapFree(pHookItem->hHeap, 0, pHookItem );
1497 return TRUE;
1499 CLEANUP:
1500 /* Release the hook table entry */
1501 HeapFree(pHookItem->hHeap, 0, pHookItem );
1503 return FALSE;
1506 /*************************************************************************
1507 * OLEMenu_IsHookInstalled
1508 * Tests if OLEMenu hooks have been installed for a thread
1510 * RETURNS: The pointer and index of the hook table entry for the tid
1511 * NULL and -1 for the index if no hooks were installed for this thread
1513 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1515 OleMenuHookItem *pHookItem;
1517 /* Do a simple linear search for an entry whose tid matches ours.
1518 * We really need a map but efficiency is not a concern here. */
1519 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1521 if ( tid == pHookItem->tid )
1522 return pHookItem;
1525 return NULL;
1528 /***********************************************************************
1529 * OLEMenu_FindMainMenuIndex
1531 * Used by OLEMenu API to find the top level group a menu item belongs to.
1532 * On success pnPos contains the index of the item in the top level menu group
1534 * RETURNS: TRUE if the ID was found, FALSE on failure
1536 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1538 INT i, nItems;
1540 nItems = GetMenuItemCount( hMainMenu );
1542 for (i = 0; i < nItems; i++)
1544 HMENU hsubmenu;
1546 /* Is the current item a submenu? */
1547 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1549 /* If the handle is the same we're done */
1550 if ( hsubmenu == hPopupMenu )
1552 if (pnPos)
1553 *pnPos = i;
1554 return TRUE;
1556 /* Recursively search without updating pnPos */
1557 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1559 if (pnPos)
1560 *pnPos = i;
1561 return TRUE;
1566 return FALSE;
1569 /***********************************************************************
1570 * OLEMenu_SetIsServerMenu
1572 * Checks whether a popup menu belongs to a shared menu group which is
1573 * owned by the server, and sets the menu descriptor state accordingly.
1574 * All menu messages from these groups should be routed to the server.
1576 * RETURNS: TRUE if the popup menu is part of a server owned group
1577 * FALSE if the popup menu is part of a container owned group
1579 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1581 UINT nPos = 0, nWidth, i;
1583 pOleMenuDescriptor->bIsServerItem = FALSE;
1585 /* Don't bother searching if the popup is the combined menu itself */
1586 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1587 return FALSE;
1589 /* Find the menu item index in the shared OLE menu that this item belongs to */
1590 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1591 return FALSE;
1593 /* The group widths array has counts for the number of elements
1594 * in the groups File, Edit, Container, Object, Window, Help.
1595 * The Edit, Object & Help groups belong to the server object
1596 * and the other three belong to the container.
1597 * Loop through the group widths and locate the group we are a member of.
1599 for ( i = 0, nWidth = 0; i < 6; i++ )
1601 nWidth += pOleMenuDescriptor->mgw.width[i];
1602 if ( nPos < nWidth )
1604 /* Odd elements are server menu widths */
1605 pOleMenuDescriptor->bIsServerItem = i%2;
1606 break;
1610 return pOleMenuDescriptor->bIsServerItem;
1613 /*************************************************************************
1614 * OLEMenu_CallWndProc
1615 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1616 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1618 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1620 LPCWPSTRUCT pMsg;
1621 HOLEMENU hOleMenu = 0;
1622 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1623 OleMenuHookItem *pHookItem = NULL;
1624 WORD fuFlags;
1626 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1628 /* Check if we're being asked to process the message */
1629 if ( HC_ACTION != code )
1630 goto NEXTHOOK;
1632 /* Retrieve the current message being dispatched from lParam */
1633 pMsg = (LPCWPSTRUCT)lParam;
1635 /* Check if the message is destined for a window we are interested in:
1636 * If the window has an OLEMenu property we may need to dispatch
1637 * the menu message to its active objects window instead. */
1639 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1640 if ( !hOleMenu )
1641 goto NEXTHOOK;
1643 /* Get the menu descriptor */
1644 pOleMenuDescriptor = GlobalLock( hOleMenu );
1645 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1646 goto NEXTHOOK;
1648 /* Process menu messages */
1649 switch( pMsg->message )
1651 case WM_INITMENU:
1653 /* Reset the menu descriptor state */
1654 pOleMenuDescriptor->bIsServerItem = FALSE;
1656 /* Send this message to the server as well */
1657 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1658 pMsg->message, pMsg->wParam, pMsg->lParam );
1659 goto NEXTHOOK;
1662 case WM_INITMENUPOPUP:
1664 /* Save the state for whether this is a server owned menu */
1665 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1666 break;
1669 case WM_MENUSELECT:
1671 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1672 if ( fuFlags & MF_SYSMENU )
1673 goto NEXTHOOK;
1675 /* Save the state for whether this is a server owned popup menu */
1676 else if ( fuFlags & MF_POPUP )
1677 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1679 break;
1682 case WM_DRAWITEM:
1684 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1685 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1686 goto NEXTHOOK; /* Not a menu message */
1688 break;
1691 default:
1692 goto NEXTHOOK;
1695 /* If the message was for the server dispatch it accordingly */
1696 if ( pOleMenuDescriptor->bIsServerItem )
1698 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1699 pMsg->message, pMsg->wParam, pMsg->lParam );
1702 NEXTHOOK:
1703 if ( pOleMenuDescriptor )
1704 GlobalUnlock( hOleMenu );
1706 /* Lookup the hook item for the current thread */
1707 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1709 /* This should never fail!! */
1710 WARN("could not retrieve hHook for current thread!\n" );
1711 return 0;
1714 /* Pass on the message to the next hooker */
1715 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1718 /*************************************************************************
1719 * OLEMenu_GetMsgProc
1720 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1721 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1723 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1725 LPMSG pMsg;
1726 HOLEMENU hOleMenu = 0;
1727 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1728 OleMenuHookItem *pHookItem = NULL;
1729 WORD wCode;
1731 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1733 /* Check if we're being asked to process a messages */
1734 if ( HC_ACTION != code )
1735 goto NEXTHOOK;
1737 /* Retrieve the current message being dispatched from lParam */
1738 pMsg = (LPMSG)lParam;
1740 /* Check if the message is destined for a window we are interested in:
1741 * If the window has an OLEMenu property we may need to dispatch
1742 * the menu message to its active objects window instead. */
1744 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1745 if ( !hOleMenu )
1746 goto NEXTHOOK;
1748 /* Process menu messages */
1749 switch( pMsg->message )
1751 case WM_COMMAND:
1753 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1754 if ( wCode )
1755 goto NEXTHOOK; /* Not a menu message */
1756 break;
1758 default:
1759 goto NEXTHOOK;
1762 /* Get the menu descriptor */
1763 pOleMenuDescriptor = GlobalLock( hOleMenu );
1764 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1765 goto NEXTHOOK;
1767 /* If the message was for the server dispatch it accordingly */
1768 if ( pOleMenuDescriptor->bIsServerItem )
1770 /* Change the hWnd in the message to the active objects hWnd.
1771 * The message loop which reads this message will automatically
1772 * dispatch it to the embedded objects window. */
1773 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1776 NEXTHOOK:
1777 if ( pOleMenuDescriptor )
1778 GlobalUnlock( hOleMenu );
1780 /* Lookup the hook item for the current thread */
1781 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1783 /* This should never fail!! */
1784 WARN("could not retrieve hHook for current thread!\n" );
1785 return FALSE;
1788 /* Pass on the message to the next hooker */
1789 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1792 /***********************************************************************
1793 * OleCreateMenuDescriptor [OLE32.@]
1794 * Creates an OLE menu descriptor for OLE to use when dispatching
1795 * menu messages and commands.
1797 * PARAMS:
1798 * hmenuCombined - Handle to the objects combined menu
1799 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1802 HOLEMENU WINAPI OleCreateMenuDescriptor(
1803 HMENU hmenuCombined,
1804 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1806 HOLEMENU hOleMenu;
1807 OleMenuDescriptor *pOleMenuDescriptor;
1808 int i;
1810 if ( !hmenuCombined || !lpMenuWidths )
1811 return 0;
1813 /* Create an OLE menu descriptor */
1814 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1815 sizeof(OleMenuDescriptor) ) ) )
1816 return 0;
1818 pOleMenuDescriptor = GlobalLock( hOleMenu );
1819 if ( !pOleMenuDescriptor )
1820 return 0;
1822 /* Initialize menu group widths and hmenu */
1823 for ( i = 0; i < 6; i++ )
1824 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1826 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1827 pOleMenuDescriptor->bIsServerItem = FALSE;
1828 GlobalUnlock( hOleMenu );
1830 return hOleMenu;
1833 /***********************************************************************
1834 * OleDestroyMenuDescriptor [OLE32.@]
1835 * Destroy the shared menu descriptor
1837 HRESULT WINAPI OleDestroyMenuDescriptor(
1838 HOLEMENU hmenuDescriptor)
1840 if ( hmenuDescriptor )
1841 GlobalFree( hmenuDescriptor );
1842 return S_OK;
1845 /***********************************************************************
1846 * OleSetMenuDescriptor [OLE32.@]
1847 * Installs or removes OLE dispatching code for the containers frame window.
1849 * PARAMS
1850 * hOleMenu Handle to composite menu descriptor
1851 * hwndFrame Handle to containers frame window
1852 * hwndActiveObject Handle to objects in-place activation window
1853 * lpFrame Pointer to IOleInPlaceFrame on containers window
1854 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1856 * RETURNS
1857 * S_OK - menu installed correctly
1858 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1860 * FIXME
1861 * The lpFrame and lpActiveObject parameters are currently ignored
1862 * OLE should install context sensitive help F1 filtering for the app when
1863 * these are non null.
1865 HRESULT WINAPI OleSetMenuDescriptor(
1866 HOLEMENU hOleMenu,
1867 HWND hwndFrame,
1868 HWND hwndActiveObject,
1869 LPOLEINPLACEFRAME lpFrame,
1870 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1872 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1874 /* Check args */
1875 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1876 return E_INVALIDARG;
1878 if ( lpFrame || lpActiveObject )
1880 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1881 hOleMenu,
1882 hwndFrame,
1883 hwndActiveObject,
1884 lpFrame,
1885 lpActiveObject);
1888 /* Set up a message hook to intercept the containers frame window messages.
1889 * The message filter is responsible for dispatching menu messages from the
1890 * shared menu which are intended for the object.
1893 if ( hOleMenu ) /* Want to install dispatching code */
1895 /* If OLEMenu hooks are already installed for this thread, fail
1896 * Note: This effectively means that OleSetMenuDescriptor cannot
1897 * be called twice in succession on the same frame window
1898 * without first calling it with a null hOleMenu to uninstall
1900 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1901 return E_FAIL;
1903 /* Get the menu descriptor */
1904 pOleMenuDescriptor = GlobalLock( hOleMenu );
1905 if ( !pOleMenuDescriptor )
1906 return E_UNEXPECTED;
1908 /* Update the menu descriptor */
1909 pOleMenuDescriptor->hwndFrame = hwndFrame;
1910 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1912 GlobalUnlock( hOleMenu );
1913 pOleMenuDescriptor = NULL;
1915 /* Add a menu descriptor windows property to the frame window */
1916 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1918 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1919 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1920 return E_FAIL;
1922 else /* Want to uninstall dispatching code */
1924 /* Uninstall the hooks */
1925 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1926 return E_FAIL;
1928 /* Remove the menu descriptor property from the frame window */
1929 RemovePropW( hwndFrame, prop_olemenuW );
1932 return S_OK;
1935 /******************************************************************************
1936 * IsAccelerator [OLE32.@]
1937 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1939 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1941 LPACCEL lpAccelTbl;
1942 int i;
1944 if(!lpMsg) return FALSE;
1945 if (!hAccel)
1947 WARN_(accel)("NULL accel handle\n");
1948 return FALSE;
1950 if((lpMsg->message != WM_KEYDOWN &&
1951 lpMsg->message != WM_SYSKEYDOWN &&
1952 lpMsg->message != WM_SYSCHAR &&
1953 lpMsg->message != WM_CHAR)) return FALSE;
1954 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1955 if (NULL == lpAccelTbl)
1957 return FALSE;
1959 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1961 WARN_(accel)("CopyAcceleratorTableW failed\n");
1962 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1963 return FALSE;
1966 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1967 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
1968 hAccel, cAccelEntries,
1969 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1970 for(i = 0; i < cAccelEntries; i++)
1972 if(lpAccelTbl[i].key != lpMsg->wParam)
1973 continue;
1975 if(lpMsg->message == WM_CHAR)
1977 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1979 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
1980 goto found;
1983 else
1985 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1987 INT mask = 0;
1988 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
1989 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1990 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1991 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1992 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1993 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1994 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1996 else
1998 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
2000 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
2001 { /* ^^ ALT pressed */
2002 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
2003 goto found;
2010 WARN_(accel)("couldn't translate accelerator key\n");
2011 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2012 return FALSE;
2014 found:
2015 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
2016 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2017 return TRUE;
2020 /***********************************************************************
2021 * ReleaseStgMedium [OLE32.@]
2023 void WINAPI ReleaseStgMedium(
2024 STGMEDIUM* pmedium)
2026 if (!pmedium) return;
2028 switch (pmedium->tymed)
2030 case TYMED_HGLOBAL:
2032 if ( (pmedium->pUnkForRelease==0) &&
2033 (pmedium->u.hGlobal!=0) )
2034 GlobalFree(pmedium->u.hGlobal);
2035 break;
2037 case TYMED_FILE:
2039 if (pmedium->u.lpszFileName!=0)
2041 if (pmedium->pUnkForRelease==0)
2043 DeleteFileW(pmedium->u.lpszFileName);
2046 CoTaskMemFree(pmedium->u.lpszFileName);
2048 break;
2050 case TYMED_ISTREAM:
2052 if (pmedium->u.pstm!=0)
2054 IStream_Release(pmedium->u.pstm);
2056 break;
2058 case TYMED_ISTORAGE:
2060 if (pmedium->u.pstg!=0)
2062 IStorage_Release(pmedium->u.pstg);
2064 break;
2066 case TYMED_GDI:
2068 if ( (pmedium->pUnkForRelease==0) &&
2069 (pmedium->u.hBitmap!=0) )
2070 DeleteObject(pmedium->u.hBitmap);
2071 break;
2073 case TYMED_MFPICT:
2075 if ( (pmedium->pUnkForRelease==0) &&
2076 (pmedium->u.hMetaFilePict!=0) )
2078 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
2079 DeleteMetaFile(pMP->hMF);
2080 GlobalUnlock(pmedium->u.hMetaFilePict);
2081 GlobalFree(pmedium->u.hMetaFilePict);
2083 break;
2085 case TYMED_ENHMF:
2087 if ( (pmedium->pUnkForRelease==0) &&
2088 (pmedium->u.hEnhMetaFile!=0) )
2090 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
2092 break;
2094 case TYMED_NULL:
2095 default:
2096 break;
2098 pmedium->tymed=TYMED_NULL;
2101 * After cleaning up, the unknown is released
2103 if (pmedium->pUnkForRelease!=0)
2105 IUnknown_Release(pmedium->pUnkForRelease);
2106 pmedium->pUnkForRelease = 0;
2110 /***
2111 * OLEDD_Initialize()
2113 * Initializes the OLE drag and drop data structures.
2115 static void OLEDD_Initialize(void)
2117 WNDCLASSW wndClass;
2119 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2120 wndClass.style = CS_GLOBALCLASS;
2121 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
2122 wndClass.cbClsExtra = 0;
2123 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
2124 wndClass.hCursor = 0;
2125 wndClass.hbrBackground = 0;
2126 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
2128 RegisterClassW (&wndClass);
2131 /***
2132 * OLEDD_DragTrackerWindowProc()
2134 * This method is the WindowProcedure of the drag n drop tracking
2135 * window. During a drag n Drop operation, an invisible window is created
2136 * to receive the user input and act upon it. This procedure is in charge
2137 * of this behavior.
2140 #define DRAG_TIMER_ID 1
2142 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
2143 HWND hwnd,
2144 UINT uMsg,
2145 WPARAM wParam,
2146 LPARAM lParam)
2148 switch (uMsg)
2150 case WM_CREATE:
2152 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
2154 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
2155 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
2157 break;
2159 case WM_TIMER:
2160 case WM_MOUSEMOVE:
2161 case WM_LBUTTONUP:
2162 case WM_MBUTTONUP:
2163 case WM_RBUTTONUP:
2164 case WM_LBUTTONDOWN:
2165 case WM_MBUTTONDOWN:
2166 case WM_RBUTTONDOWN:
2168 TrackerWindowInfo *trackerInfo = (TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0);
2169 if (trackerInfo->trackingDone) break;
2170 OLEDD_TrackStateChange(trackerInfo);
2171 break;
2173 case WM_DESTROY:
2175 KillTimer(hwnd, DRAG_TIMER_ID);
2176 break;
2181 * This is a window proc after all. Let's call the default.
2183 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2186 static void drag_enter( TrackerWindowInfo *info, HWND new_target )
2188 HRESULT hr;
2190 info->curTargetHWND = new_target;
2192 while (new_target && !is_droptarget( new_target ))
2193 new_target = GetParent( new_target );
2195 info->curDragTarget = get_droptarget_pointer( new_target );
2197 if (info->curDragTarget)
2199 *info->pdwEffect = info->dwOKEffect;
2200 hr = IDropTarget_DragEnter( info->curDragTarget, info->dataObject,
2201 info->dwKeyState, info->curMousePos,
2202 info->pdwEffect );
2203 *info->pdwEffect &= info->dwOKEffect;
2205 /* failed DragEnter() means invalid target */
2206 if (hr != S_OK)
2208 IDropTarget_Release( info->curDragTarget );
2209 info->curDragTarget = NULL;
2210 info->curTargetHWND = NULL;
2215 static void drag_end( TrackerWindowInfo *info )
2217 HRESULT hr;
2219 info->trackingDone = TRUE;
2220 ReleaseCapture();
2222 if (info->curDragTarget)
2224 if (info->returnValue == DRAGDROP_S_DROP &&
2225 *info->pdwEffect != DROPEFFECT_NONE)
2227 *info->pdwEffect = info->dwOKEffect;
2228 hr = IDropTarget_Drop( info->curDragTarget, info->dataObject, info->dwKeyState,
2229 info->curMousePos, info->pdwEffect );
2230 *info->pdwEffect &= info->dwOKEffect;
2232 if (FAILED( hr ))
2233 info->returnValue = hr;
2235 else
2237 IDropTarget_DragLeave( info->curDragTarget );
2238 *info->pdwEffect = DROPEFFECT_NONE;
2240 IDropTarget_Release( info->curDragTarget );
2241 info->curDragTarget = NULL;
2243 else
2244 *info->pdwEffect = DROPEFFECT_NONE;
2247 static HRESULT give_feedback( TrackerWindowInfo *info )
2249 HRESULT hr;
2250 int res;
2251 HCURSOR cur;
2253 if (info->curDragTarget == NULL)
2254 *info->pdwEffect = DROPEFFECT_NONE;
2256 hr = IDropSource_GiveFeedback( info->dropSource, *info->pdwEffect );
2258 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2260 if (*info->pdwEffect & DROPEFFECT_MOVE)
2261 res = CURSOR_MOVE;
2262 else if (*info->pdwEffect & DROPEFFECT_COPY)
2263 res = CURSOR_COPY;
2264 else if (*info->pdwEffect & DROPEFFECT_LINK)
2265 res = CURSOR_LINK;
2266 else
2267 res = CURSOR_NODROP;
2269 cur = LoadCursorW( hProxyDll, MAKEINTRESOURCEW( res ) );
2270 SetCursor( cur );
2273 return hr;
2276 /***
2277 * OLEDD_TrackStateChange()
2279 * This method is invoked while a drag and drop operation is in effect.
2281 * params:
2282 * trackerInfo - Pointer to the structure identifying the
2283 * drag & drop operation that is currently
2284 * active.
2286 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2288 HWND hwndNewTarget = 0;
2289 POINT pt;
2292 * This method may be called from QueryContinueDrag again,
2293 * (i.e. by running message loop) so avoid recursive call chain.
2295 if (trackerInfo->inTrackCall) return;
2296 trackerInfo->inTrackCall = TRUE;
2299 * Get the handle of the window under the mouse
2301 pt.x = trackerInfo->curMousePos.x;
2302 pt.y = trackerInfo->curMousePos.y;
2303 hwndNewTarget = WindowFromPoint(pt);
2305 trackerInfo->returnValue = IDropSource_QueryContinueDrag(trackerInfo->dropSource,
2306 trackerInfo->escPressed,
2307 trackerInfo->dwKeyState);
2309 if (trackerInfo->curTargetHWND != hwndNewTarget &&
2310 (trackerInfo->returnValue == S_OK ||
2311 trackerInfo->returnValue == DRAGDROP_S_DROP))
2313 if (trackerInfo->curDragTarget)
2315 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2316 IDropTarget_Release(trackerInfo->curDragTarget);
2317 trackerInfo->curDragTarget = NULL;
2318 trackerInfo->curTargetHWND = NULL;
2321 if (hwndNewTarget)
2322 drag_enter( trackerInfo, hwndNewTarget );
2324 give_feedback( trackerInfo );
2328 if (trackerInfo->returnValue == S_OK)
2330 if (trackerInfo->curDragTarget)
2332 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2333 IDropTarget_DragOver(trackerInfo->curDragTarget,
2334 trackerInfo->dwKeyState,
2335 trackerInfo->curMousePos,
2336 trackerInfo->pdwEffect);
2337 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
2339 give_feedback( trackerInfo );
2341 else
2342 drag_end( trackerInfo );
2344 trackerInfo->inTrackCall = FALSE;
2347 /***
2348 * OLEDD_GetButtonState()
2350 * This method will use the current state of the keyboard to build
2351 * a button state mask equivalent to the one passed in the
2352 * WM_MOUSEMOVE wParam.
2354 static DWORD OLEDD_GetButtonState(void)
2356 BYTE keyboardState[256];
2357 DWORD keyMask = 0;
2359 GetKeyboardState(keyboardState);
2361 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2362 keyMask |= MK_SHIFT;
2364 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2365 keyMask |= MK_CONTROL;
2367 if ( (keyboardState[VK_MENU] & 0x80) !=0)
2368 keyMask |= MK_ALT;
2370 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2371 keyMask |= MK_LBUTTON;
2373 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2374 keyMask |= MK_RBUTTON;
2376 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2377 keyMask |= MK_MBUTTON;
2379 return keyMask;
2382 /***
2383 * OLEDD_GetButtonState()
2385 * This method will read the default value of the registry key in
2386 * parameter and extract a DWORD value from it. The registry key value
2387 * can be in a string key or a DWORD key.
2389 * params:
2390 * regKey - Key to read the default value from
2391 * pdwValue - Pointer to the location where the DWORD
2392 * value is returned. This value is not modified
2393 * if the value is not found.
2396 static void OLEUTL_ReadRegistryDWORDValue(
2397 HKEY regKey,
2398 DWORD* pdwValue)
2400 WCHAR buffer[20];
2401 DWORD cbData = sizeof(buffer);
2402 DWORD dwKeyType;
2403 LONG lres;
2405 lres = RegQueryValueExW(regKey,
2406 emptyW,
2407 NULL,
2408 &dwKeyType,
2409 (LPBYTE)buffer,
2410 &cbData);
2412 if (lres==ERROR_SUCCESS)
2414 switch (dwKeyType)
2416 case REG_DWORD:
2417 *pdwValue = *(DWORD*)buffer;
2418 break;
2419 case REG_EXPAND_SZ:
2420 case REG_MULTI_SZ:
2421 case REG_SZ:
2422 *pdwValue = wcstoul(buffer, NULL, 10);
2423 break;
2428 /******************************************************************************
2429 * OleDraw (OLE32.@)
2431 * The operation of this function is documented literally in the WinAPI
2432 * documentation to involve a QueryInterface for the IViewObject interface,
2433 * followed by a call to IViewObject::Draw.
2435 HRESULT WINAPI OleDraw(
2436 IUnknown *pUnk,
2437 DWORD dwAspect,
2438 HDC hdcDraw,
2439 LPCRECT rect)
2441 HRESULT hres;
2442 IViewObject *viewobject;
2444 if (!pUnk) return E_INVALIDARG;
2446 hres = IUnknown_QueryInterface(pUnk,
2447 &IID_IViewObject,
2448 (void**)&viewobject);
2449 if (SUCCEEDED(hres))
2451 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, (RECTL*)rect, 0, 0, 0);
2452 IViewObject_Release(viewobject);
2453 return hres;
2455 else
2456 return DV_E_NOIVIEWOBJECT;
2459 /***********************************************************************
2460 * OleTranslateAccelerator [OLE32.@]
2462 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2463 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2465 WORD wID;
2467 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2469 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2470 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2472 return S_FALSE;
2475 /******************************************************************************
2476 * OleCreate [OLE32.@]
2479 HRESULT WINAPI OleCreate(
2480 REFCLSID rclsid,
2481 REFIID riid,
2482 DWORD renderopt,
2483 LPFORMATETC pFormatEtc,
2484 LPOLECLIENTSITE pClientSite,
2485 LPSTORAGE pStg,
2486 LPVOID* ppvObj)
2488 HRESULT hres;
2489 IUnknown * pUnk = NULL;
2490 IOleObject *pOleObject = NULL;
2492 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2493 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2495 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2497 if (SUCCEEDED(hres))
2498 hres = IStorage_SetClass(pStg, rclsid);
2500 if (pClientSite && SUCCEEDED(hres))
2502 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2503 if (SUCCEEDED(hres))
2505 DWORD dwStatus;
2506 IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2510 if (SUCCEEDED(hres))
2512 IPersistStorage * pPS;
2513 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2515 TRACE("trying to set stg %p\n", pStg);
2516 hres = IPersistStorage_InitNew(pPS, pStg);
2517 TRACE("-- result 0x%08x\n", hres);
2518 IPersistStorage_Release(pPS);
2522 if (pClientSite && SUCCEEDED(hres))
2524 TRACE("trying to set clientsite %p\n", pClientSite);
2525 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2526 TRACE("-- result 0x%08x\n", hres);
2529 if (pOleObject)
2530 IOleObject_Release(pOleObject);
2532 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2533 SUCCEEDED(hres))
2535 hres = OleRun(pUnk);
2536 if (SUCCEEDED(hres))
2538 IOleCache *pOleCache;
2540 if (SUCCEEDED(IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache)))
2542 DWORD dwConnection;
2543 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2544 FORMATETC pfe;
2545 pfe.cfFormat = 0;
2546 pfe.ptd = NULL;
2547 pfe.dwAspect = DVASPECT_CONTENT;
2548 pfe.lindex = -1;
2549 pfe.tymed = TYMED_NULL;
2550 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2552 else
2553 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2554 IOleCache_Release(pOleCache);
2559 if (FAILED(hres) && pUnk)
2561 IUnknown_Release(pUnk);
2562 pUnk = NULL;
2565 *ppvObj = pUnk;
2567 TRACE("-- %p\n", pUnk);
2568 return hres;
2571 /******************************************************************************
2572 * OleGetAutoConvert [OLE32.@]
2574 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2576 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2577 HKEY hkey = NULL;
2578 WCHAR buf[CHARS_IN_GUID];
2579 LONG len;
2580 HRESULT res = S_OK;
2582 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2583 if (FAILED(res))
2584 goto done;
2586 len = sizeof(buf);
2587 if (RegQueryValueW(hkey, NULL, buf, &len))
2589 res = REGDB_E_KEYMISSING;
2590 goto done;
2592 res = CLSIDFromString(buf, pClsidNew);
2593 done:
2594 if (hkey) RegCloseKey(hkey);
2595 return res;
2598 /******************************************************************************
2599 * OleSetAutoConvert [OLE32.@]
2601 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2603 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2604 HKEY hkey = NULL;
2605 WCHAR szClsidNew[CHARS_IN_GUID];
2606 HRESULT res = S_OK;
2608 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2610 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2611 if (FAILED(res))
2612 goto done;
2613 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2614 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (lstrlenW(szClsidNew)+1) * sizeof(WCHAR)))
2616 res = REGDB_E_WRITEREGDB;
2617 goto done;
2620 done:
2621 if (hkey) RegCloseKey(hkey);
2622 return res;
2625 /******************************************************************************
2626 * OleDoAutoConvert [OLE32.@]
2628 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2630 WCHAR *user_type_old, *user_type_new;
2631 CLIPFORMAT cf;
2632 STATSTG stat;
2633 CLSID clsid;
2634 HRESULT hr;
2636 TRACE("(%p, %p)\n", pStg, pClsidNew);
2638 *pClsidNew = CLSID_NULL;
2639 if(!pStg)
2640 return E_INVALIDARG;
2641 hr = IStorage_Stat(pStg, &stat, STATFLAG_NONAME);
2642 if(FAILED(hr))
2643 return hr;
2645 *pClsidNew = stat.clsid;
2646 hr = OleGetAutoConvert(&stat.clsid, &clsid);
2647 if(FAILED(hr))
2648 return hr;
2650 hr = IStorage_SetClass(pStg, &clsid);
2651 if(FAILED(hr))
2652 return hr;
2654 hr = ReadFmtUserTypeStg(pStg, &cf, &user_type_old);
2655 if(FAILED(hr)) {
2656 cf = 0;
2657 user_type_new = NULL;
2660 hr = OleRegGetUserType(&clsid, USERCLASSTYPE_FULL, &user_type_new);
2661 if(FAILED(hr))
2662 user_type_new = NULL;
2664 hr = WriteFmtUserTypeStg(pStg, cf, user_type_new);
2665 CoTaskMemFree(user_type_new);
2666 if(FAILED(hr))
2668 CoTaskMemFree(user_type_old);
2669 IStorage_SetClass(pStg, &stat.clsid);
2670 return hr;
2673 hr = SetConvertStg(pStg, TRUE);
2674 if(FAILED(hr))
2676 WriteFmtUserTypeStg(pStg, cf, user_type_old);
2677 IStorage_SetClass(pStg, &stat.clsid);
2679 else
2680 *pClsidNew = clsid;
2681 CoTaskMemFree(user_type_old);
2682 return hr;
2685 /******************************************************************************
2686 * OleIsRunning [OLE32.@]
2688 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2690 IRunnableObject *pRunnable;
2691 HRESULT hr;
2692 BOOL running;
2694 TRACE("(%p)\n", object);
2696 if (!object) return FALSE;
2698 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2699 if (FAILED(hr))
2700 return TRUE;
2701 running = IRunnableObject_IsRunning(pRunnable);
2702 IRunnableObject_Release(pRunnable);
2703 return running;
2706 /***********************************************************************
2707 * OleNoteObjectVisible [OLE32.@]
2709 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2711 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2712 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2715 /***********************************************************************
2716 * PropSysAllocString [OLE32.@]
2717 * NOTES
2718 * Forward to oleaut32.
2720 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2722 return SysAllocString(str);
2725 /***********************************************************************
2726 * PropSysFreeString [OLE32.@]
2727 * NOTES
2728 * Forward to oleaut32.
2730 void WINAPI PropSysFreeString(LPOLESTR str)
2732 SysFreeString(str);