fdopen: don't rewind the file after creating the FILE* handle. Added
[wine] / dlls / ole32 / ole2.c
1
2 /*
3  *      OLE2 library
4  *
5  *      Copyright 1995  Martin von Loewis
6  *      Copyright 1999  Francis Beaudet
7  *      Copyright 1999  Noel Borthwick
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "commctrl.h"
32 #include "ole2.h"
33 #include "ole2ver.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winuser.h"
38 #include "winreg.h"
39 #include "wownt32.h"
40
41 #include "wine/obj_clientserver.h"
42 #include "wine/winbase16.h"
43 #include "wine/wingdi16.h"
44 #include "wine/winuser16.h"
45 #include "ole32_main.h"
46
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(ole);
50 WINE_DECLARE_DEBUG_CHANNEL(accel);
51
52 #define HICON_16(h32)           (LOWORD(h32))
53 #define HICON_32(h16)           ((HICON)(ULONG_PTR)(h16))
54 #define HINSTANCE_32(h16)       ((HINSTANCE)(ULONG_PTR)(h16))
55
56 /******************************************************************************
57  * These are static/global variables and internal data structures that the
58  * OLE module uses to maintain it's state.
59  */
60 typedef struct tagDropTargetNode
61 {
62   HWND          hwndTarget;
63   IDropTarget*    dropTarget;
64   struct tagDropTargetNode* prevDropTarget;
65   struct tagDropTargetNode* nextDropTarget;
66 } DropTargetNode;
67
68 typedef struct tagTrackerWindowInfo
69 {
70   IDataObject* dataObject;
71   IDropSource* dropSource;
72   DWORD        dwOKEffect;
73   DWORD*       pdwEffect;
74   BOOL       trackingDone;
75   HRESULT      returnValue;
76
77   BOOL       escPressed;
78   HWND       curDragTargetHWND;
79   IDropTarget* curDragTarget;
80 } TrackerWindowInfo;
81
82 typedef struct tagOleMenuDescriptor  /* OleMenuDescriptor */
83 {
84   HWND               hwndFrame;         /* The containers frame window */
85   HWND               hwndActiveObject;  /* The active objects window */
86   OLEMENUGROUPWIDTHS mgw;               /* OLE menu group widths for the shared menu */
87   HMENU              hmenuCombined;     /* The combined menu */
88   BOOL               bIsServerItem;     /* True if the currently open popup belongs to the server */
89 } OleMenuDescriptor;
90
91 typedef struct tagOleMenuHookItem   /* OleMenu hook item in per thread hook list */
92 {
93   DWORD tid;                /* Thread Id  */
94   HANDLE hHeap;             /* Heap this is allocated from */
95   HHOOK GetMsg_hHook;       /* message hook for WH_GETMESSAGE */
96   HHOOK CallWndProc_hHook;  /* message hook for WH_CALLWNDPROC */
97   struct tagOleMenuHookItem *next;
98 } OleMenuHookItem;
99
100 static OleMenuHookItem *hook_list;
101
102 /*
103  * This is the lock count on the OLE library. It is controlled by the
104  * OLEInitialize/OLEUninitialize methods.
105  */
106 static ULONG OLE_moduleLockCount = 0;
107
108 /*
109  * Name of our registered window class.
110  */
111 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
112
113 /*
114  * This is the head of the Drop target container.
115  */
116 static DropTargetNode* targetListHead = NULL;
117
118 /******************************************************************************
119  * These are the prototypes of miscelaneous utility methods
120  */
121 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
122
123 /******************************************************************************
124  * These are the prototypes of the utility methods used to manage a shared menu
125  */
126 static void OLEMenu_Initialize();
127 static void OLEMenu_UnInitialize();
128 BOOL OLEMenu_InstallHooks( DWORD tid );
129 BOOL OLEMenu_UnInstallHooks( DWORD tid );
130 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
131 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
132 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
133 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
134 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
135
136 /******************************************************************************
137  * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
138  */
139 void OLEClipbrd_UnInitialize();
140 void OLEClipbrd_Initialize();
141
142 /******************************************************************************
143  * These are the prototypes of the utility methods used for OLE Drag n Drop
144  */
145 static void            OLEDD_Initialize();
146 static void            OLEDD_UnInitialize();
147 static void            OLEDD_InsertDropTarget(
148                          DropTargetNode* nodeToAdd);
149 static DropTargetNode* OLEDD_ExtractDropTarget(
150                          HWND hwndOfTarget);
151 static DropTargetNode* OLEDD_FindDropTarget(
152                          HWND hwndOfTarget);
153 static LRESULT WINAPI  OLEDD_DragTrackerWindowProc(
154                          HWND   hwnd,
155                          UINT   uMsg,
156                          WPARAM wParam,
157                          LPARAM   lParam);
158 static void OLEDD_TrackMouseMove(
159                          TrackerWindowInfo* trackerInfo,
160                          POINT            mousePos,
161                          DWORD              keyState);
162 static void OLEDD_TrackStateChange(
163                          TrackerWindowInfo* trackerInfo,
164                          POINT            mousePos,
165                          DWORD              keyState);
166 static DWORD OLEDD_GetButtonState();
167
168
169 /******************************************************************************
170  *              OleBuildVersion [OLE2.1]
171  *              OleBuildVersion [OLE32.84]
172  */
173 DWORD WINAPI OleBuildVersion(void)
174 {
175     TRACE("Returning version %d, build %d.\n", rmm, rup);
176     return (rmm<<16)+rup;
177 }
178
179 /***********************************************************************
180  *           OleInitialize       (OLE2.2)
181  *           OleInitialize       (OLE32.108)
182  */
183 HRESULT WINAPI OleInitialize(LPVOID reserved)
184 {
185   HRESULT hr;
186
187   TRACE("(%p)\n", reserved);
188
189   /*
190    * The first duty of the OleInitialize is to initialize the COM libraries.
191    */
192   hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
193
194   /*
195    * If the CoInitializeEx call failed, the OLE libraries can't be
196    * initialized.
197    */
198   if (FAILED(hr))
199     return hr;
200
201   /*
202    * Then, it has to initialize the OLE specific modules.
203    * This includes:
204    *     Clipboard
205    *     Drag and Drop
206    *     Object linking and Embedding
207    *     In-place activation
208    */
209   if (OLE_moduleLockCount==0)
210   {
211     /*
212      * Initialize the libraries.
213      */
214     TRACE("() - Initializing the OLE libraries\n");
215
216     /*
217      * OLE Clipboard
218      */
219     OLEClipbrd_Initialize();
220
221     /*
222      * Drag and Drop
223      */
224     OLEDD_Initialize();
225
226     /*
227      * OLE shared menu
228      */
229     OLEMenu_Initialize();
230   }
231
232   /*
233    * Then, we increase the lock count on the OLE module.
234    */
235   OLE_moduleLockCount++;
236
237   return hr;
238 }
239
240 /******************************************************************************
241  *              CoGetCurrentProcess     [COMPOBJ.34]
242  *              CoGetCurrentProcess     [OLE32.18]
243  *
244  * NOTES
245  *   Is DWORD really the correct return type for this function?
246  */
247 DWORD WINAPI CoGetCurrentProcess(void)
248 {
249         return GetCurrentProcessId();
250 }
251
252 /******************************************************************************
253  *              OleUninitialize [OLE2.3]
254  *              OleUninitialize [OLE32.131]
255  */
256 void WINAPI OleUninitialize(void)
257 {
258   TRACE("()\n");
259
260   /*
261    * Decrease the lock count on the OLE module.
262    */
263   OLE_moduleLockCount--;
264
265   /*
266    * If we hit the bottom of the lock stack, free the libraries.
267    */
268   if (OLE_moduleLockCount==0)
269   {
270     /*
271      * Actually free the libraries.
272      */
273     TRACE("() - Freeing the last reference count\n");
274
275     /*
276      * OLE Clipboard
277      */
278     OLEClipbrd_UnInitialize();
279
280     /*
281      * Drag and Drop
282      */
283     OLEDD_UnInitialize();
284
285     /*
286      * OLE shared menu
287      */
288     OLEMenu_UnInitialize();
289   }
290
291   /*
292    * Then, uninitialize the COM libraries.
293    */
294   CoUninitialize();
295 }
296
297 /******************************************************************************
298  *              CoRegisterMessageFilter [OLE32.38]
299  */
300 HRESULT WINAPI CoRegisterMessageFilter(
301     LPMESSAGEFILTER lpMessageFilter,    /* [in] Pointer to interface */
302     LPMESSAGEFILTER *lplpMessageFilter  /* [out] Indirect pointer to prior instance if non-NULL */
303 ) {
304     FIXME("stub\n");
305     if (lplpMessageFilter) {
306         *lplpMessageFilter = NULL;
307     }
308     return S_OK;
309 }
310
311 /******************************************************************************
312  *              OleInitializeWOW        [OLE32.109]
313  */
314 HRESULT WINAPI OleInitializeWOW(DWORD x) {
315         FIXME("(0x%08lx),stub!\n",x);
316         return 0;
317 }
318
319 /***********************************************************************
320  *           RegisterDragDrop (OLE2.35)
321  */
322 HRESULT WINAPI RegisterDragDrop16(
323         HWND16 hwnd,
324         LPDROPTARGET pDropTarget
325 ) {
326         FIXME("(0x%04x,%p),stub!\n",hwnd,pDropTarget);
327         return S_OK;
328 }
329
330 /***********************************************************************
331  *           RegisterDragDrop (OLE32.139)
332  */
333 HRESULT WINAPI RegisterDragDrop(
334         HWND hwnd,
335         LPDROPTARGET pDropTarget)
336 {
337   DropTargetNode* dropTargetInfo;
338
339   TRACE("(%p,%p)\n", hwnd, pDropTarget);
340
341   /*
342    * First, check if the window is already registered.
343    */
344   dropTargetInfo = OLEDD_FindDropTarget(hwnd);
345
346   if (dropTargetInfo!=NULL)
347     return DRAGDROP_E_ALREADYREGISTERED;
348
349   /*
350    * If it's not there, we can add it. We first create a node for it.
351    */
352   dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
353
354   if (dropTargetInfo==NULL)
355     return E_OUTOFMEMORY;
356
357   dropTargetInfo->hwndTarget     = hwnd;
358   dropTargetInfo->prevDropTarget = NULL;
359   dropTargetInfo->nextDropTarget = NULL;
360
361   /*
362    * Don't forget that this is an interface pointer, need to nail it down since
363    * we keep a copy of it.
364    */
365   dropTargetInfo->dropTarget  = pDropTarget;
366   IDropTarget_AddRef(dropTargetInfo->dropTarget);
367
368   OLEDD_InsertDropTarget(dropTargetInfo);
369
370         return S_OK;
371 }
372
373 /***********************************************************************
374  *           RevokeDragDrop (OLE2.36)
375  */
376 HRESULT WINAPI RevokeDragDrop16(
377         HWND16 hwnd
378 ) {
379         FIXME("(0x%04x),stub!\n",hwnd);
380         return S_OK;
381 }
382
383 /***********************************************************************
384  *           RevokeDragDrop (OLE32.141)
385  */
386 HRESULT WINAPI RevokeDragDrop(
387         HWND hwnd)
388 {
389   DropTargetNode* dropTargetInfo;
390
391   TRACE("(%p)\n", hwnd);
392
393   /*
394    * First, check if the window is already registered.
395    */
396   dropTargetInfo = OLEDD_ExtractDropTarget(hwnd);
397
398   /*
399    * If it ain't in there, it's an error.
400    */
401   if (dropTargetInfo==NULL)
402     return DRAGDROP_E_NOTREGISTERED;
403
404   /*
405    * If it's in there, clean-up it's used memory and
406    * references
407    */
408   IDropTarget_Release(dropTargetInfo->dropTarget);
409   HeapFree(GetProcessHeap(), 0, dropTargetInfo);
410
411         return S_OK;
412 }
413
414 /***********************************************************************
415  *           OleRegGetUserType (OLE32.122)
416  *
417  * This implementation of OleRegGetUserType ignores the dwFormOfType
418  * parameter and always returns the full name of the object. This is
419  * not too bad since this is the case for many objects because of the
420  * way they are registered.
421  */
422 HRESULT WINAPI OleRegGetUserType(
423         REFCLSID clsid,
424         DWORD dwFormOfType,
425         LPOLESTR* pszUserType)
426 {
427   char    keyName[60];
428   DWORD   dwKeyType;
429   DWORD   cbData;
430   HKEY    clsidKey;
431   LONG    hres;
432   LPBYTE  buffer;
433   HRESULT retVal;
434   /*
435    * Initialize the out parameter.
436    */
437   *pszUserType = NULL;
438
439   /*
440    * Build the key name we're looking for
441    */
442   sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
443            clsid->Data1, clsid->Data2, clsid->Data3,
444            clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
445            clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
446
447   TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
448
449   /*
450    * Open the class id Key
451    */
452   hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
453                      keyName,
454                      &clsidKey);
455
456   if (hres != ERROR_SUCCESS)
457     return REGDB_E_CLASSNOTREG;
458
459   /*
460    * Retrieve the size of the name string.
461    */
462   cbData = 0;
463
464   hres = RegQueryValueExA(clsidKey,
465                           "",
466                           NULL,
467                           &dwKeyType,
468                           NULL,
469                           &cbData);
470
471   if (hres!=ERROR_SUCCESS)
472   {
473     RegCloseKey(clsidKey);
474     return REGDB_E_READREGDB;
475   }
476
477   /*
478    * Allocate a buffer for the registry value.
479    */
480   *pszUserType = CoTaskMemAlloc(cbData*2);
481
482   if (*pszUserType==NULL)
483   {
484     RegCloseKey(clsidKey);
485     return E_OUTOFMEMORY;
486   }
487
488   buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
489
490   if (buffer == NULL)
491   {
492     RegCloseKey(clsidKey);
493     CoTaskMemFree(*pszUserType);
494     *pszUserType=NULL;
495     return E_OUTOFMEMORY;
496   }
497
498   hres = RegQueryValueExA(clsidKey,
499                           "",
500                           NULL,
501                           &dwKeyType,
502                           buffer,
503                           &cbData);
504
505   RegCloseKey(clsidKey);
506
507
508   if (hres!=ERROR_SUCCESS)
509   {
510     CoTaskMemFree(*pszUserType);
511     *pszUserType=NULL;
512
513     retVal = REGDB_E_READREGDB;
514   }
515   else
516   {
517     MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
518     retVal = S_OK;
519   }
520   HeapFree(GetProcessHeap(), 0, buffer);
521
522   return retVal;
523 }
524
525 /***********************************************************************
526  * DoDragDrop [OLE32.65]
527  */
528 HRESULT WINAPI DoDragDrop (
529   IDataObject *pDataObject,  /* [in] ptr to the data obj           */
530   IDropSource* pDropSource,  /* [in] ptr to the source obj         */
531   DWORD       dwOKEffect,    /* [in] effects allowed by the source */
532   DWORD       *pdwEffect)    /* [out] ptr to effects of the source */
533 {
534   TrackerWindowInfo trackerInfo;
535   HWND            hwndTrackWindow;
536   MSG             msg;
537
538   TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
539
540   /*
541    * Setup the drag n drop tracking window.
542    */
543   trackerInfo.dataObject        = pDataObject;
544   trackerInfo.dropSource        = pDropSource;
545   trackerInfo.dwOKEffect        = dwOKEffect;
546   trackerInfo.pdwEffect         = pdwEffect;
547   trackerInfo.trackingDone      = FALSE;
548   trackerInfo.escPressed        = FALSE;
549   trackerInfo.curDragTargetHWND = 0;
550   trackerInfo.curDragTarget     = 0;
551
552   hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
553                                     "TrackerWindow",
554                                     WS_POPUP,
555                                     CW_USEDEFAULT, CW_USEDEFAULT,
556                                     CW_USEDEFAULT, CW_USEDEFAULT,
557                                     0,
558                                     0,
559                                     0,
560                                     (LPVOID)&trackerInfo);
561
562   if (hwndTrackWindow!=0)
563   {
564     /*
565      * Capture the mouse input
566      */
567     SetCapture(hwndTrackWindow);
568
569     /*
570      * Pump messages. All mouse input should go the the capture window.
571      */
572     while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
573     {
574       if ( (msg.message >= WM_KEYFIRST) &&
575            (msg.message <= WM_KEYLAST) )
576       {
577         /*
578          * When keyboard messages are sent to windows on this thread, we
579          * want to ignore notify the drop source that the state changed.
580          * in the case of the Escape key, we also notify the drop source
581          * we give it a special meaning.
582          */
583         if ( (msg.message==WM_KEYDOWN) &&
584              (msg.wParam==VK_ESCAPE) )
585         {
586           trackerInfo.escPressed = TRUE;
587         }
588
589         /*
590          * Notify the drop source.
591          */
592         OLEDD_TrackStateChange(&trackerInfo,
593                                msg.pt,
594                                OLEDD_GetButtonState());
595       }
596       else
597       {
598         /*
599          * Dispatch the messages only when it's not a keyboard message.
600          */
601         DispatchMessageA(&msg);
602       }
603     }
604
605     /*
606      * Destroy the temporary window.
607      */
608     DestroyWindow(hwndTrackWindow);
609
610     return trackerInfo.returnValue;
611   }
612
613   return E_FAIL;
614 }
615
616 /***********************************************************************
617  * OleQueryLinkFromData [OLE32.118]
618  */
619 HRESULT WINAPI OleQueryLinkFromData(
620   IDataObject* pSrcDataObject)
621 {
622   FIXME("(%p),stub!\n", pSrcDataObject);
623   return S_OK;
624 }
625
626 /***********************************************************************
627  * OleRegGetMiscStatus [OLE32.121]
628  */
629 HRESULT WINAPI OleRegGetMiscStatus(
630   REFCLSID clsid,
631   DWORD    dwAspect,
632   DWORD*   pdwStatus)
633 {
634   char    keyName[60];
635   HKEY    clsidKey;
636   HKEY    miscStatusKey;
637   HKEY    aspectKey;
638   LONG    result;
639
640   /*
641    * Initialize the out parameter.
642    */
643   *pdwStatus = 0;
644
645   /*
646    * Build the key name we're looking for
647    */
648   sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
649            clsid->Data1, clsid->Data2, clsid->Data3,
650            clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
651            clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
652
653   TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
654
655   /*
656    * Open the class id Key
657    */
658   result = RegOpenKeyA(HKEY_CLASSES_ROOT,
659                        keyName,
660                        &clsidKey);
661
662   if (result != ERROR_SUCCESS)
663     return REGDB_E_CLASSNOTREG;
664
665   /*
666    * Get the MiscStatus
667    */
668   result = RegOpenKeyA(clsidKey,
669                        "MiscStatus",
670                        &miscStatusKey);
671
672
673   if (result != ERROR_SUCCESS)
674   {
675     RegCloseKey(clsidKey);
676     return REGDB_E_READREGDB;
677   }
678
679   /*
680    * Read the default value
681    */
682   OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
683
684   /*
685    * Open the key specific to the requested aspect.
686    */
687   sprintf(keyName, "%ld", dwAspect);
688
689   result = RegOpenKeyA(miscStatusKey,
690                        keyName,
691                        &aspectKey);
692
693   if (result == ERROR_SUCCESS)
694   {
695     OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
696     RegCloseKey(aspectKey);
697   }
698
699   /*
700    * Cleanup
701    */
702   RegCloseKey(miscStatusKey);
703   RegCloseKey(clsidKey);
704
705   return S_OK;
706 }
707
708 /******************************************************************************
709  *              OleSetContainedObject        [OLE32.128]
710  */
711 HRESULT WINAPI OleSetContainedObject(
712   LPUNKNOWN pUnknown,
713   BOOL      fContained)
714 {
715   IRunnableObject* runnable = NULL;
716   HRESULT          hres;
717
718   TRACE("(%p,%x), stub!\n", pUnknown, fContained);
719
720   hres = IUnknown_QueryInterface(pUnknown,
721                                  &IID_IRunnableObject,
722                                  (void**)&runnable);
723
724   if (SUCCEEDED(hres))
725   {
726     hres = IRunnableObject_SetContainedObject(runnable, fContained);
727
728     IRunnableObject_Release(runnable);
729
730     return hres;
731   }
732
733   return S_OK;
734 }
735
736 /******************************************************************************
737  *              OleLoad        [OLE32.112]
738  */
739 HRESULT WINAPI OleLoad(
740   LPSTORAGE       pStg,
741   REFIID          riid,
742   LPOLECLIENTSITE pClientSite,
743   LPVOID*         ppvObj)
744 {
745   IPersistStorage* persistStorage = NULL;
746   IOleObject*      oleObject      = NULL;
747   STATSTG          storageInfo;
748   HRESULT          hres;
749
750   TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
751
752   /*
753    * TODO, Conversion ... OleDoAutoConvert
754    */
755
756   /*
757    * Get the class ID for the object.
758    */
759   hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
760
761   /*
762    * Now, try and create the handler for the object
763    */
764   hres = CoCreateInstance(&storageInfo.clsid,
765                           NULL,
766                           CLSCTX_INPROC_HANDLER,
767                           &IID_IOleObject,
768                           (void**)&oleObject);
769
770   /*
771    * If that fails, as it will most times, load the default
772    * OLE handler.
773    */
774   if (FAILED(hres))
775   {
776     hres = OleCreateDefaultHandler(&storageInfo.clsid,
777                                    NULL,
778                                    &IID_IOleObject,
779                                    (void**)&oleObject);
780   }
781
782   /*
783    * If we couldn't find a handler... this is bad. Abort the whole thing.
784    */
785   if (FAILED(hres))
786     return hres;
787
788   /*
789    * Inform the new object of it's client site.
790    */
791   hres = IOleObject_SetClientSite(oleObject, pClientSite);
792
793   /*
794    * Initialize the object with it's IPersistStorage interface.
795    */
796   hres = IOleObject_QueryInterface(oleObject,
797                                    &IID_IPersistStorage,
798                                    (void**)&persistStorage);
799
800   if (SUCCEEDED(hres))
801   {
802     IPersistStorage_Load(persistStorage, pStg);
803
804     IPersistStorage_Release(persistStorage);
805     persistStorage = NULL;
806   }
807
808   /*
809    * Return the requested interface to the caller.
810    */
811   hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
812
813   /*
814    * Cleanup interfaces used internally
815    */
816   IOleObject_Release(oleObject);
817
818   return hres;
819 }
820
821 /***********************************************************************
822  *           OleSave     [OLE32.124]
823  */
824 HRESULT WINAPI OleSave(
825   LPPERSISTSTORAGE pPS,
826   LPSTORAGE        pStg,
827   BOOL             fSameAsLoad)
828 {
829   HRESULT hres;
830   CLSID   objectClass;
831
832   TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
833
834   /*
835    * First, we transfer the class ID (if available)
836    */
837   hres = IPersistStorage_GetClassID(pPS, &objectClass);
838
839   if (SUCCEEDED(hres))
840   {
841     WriteClassStg(pStg, &objectClass);
842   }
843
844   /*
845    * Then, we ask the object to save itself to the
846    * storage. If it is successful, we commit the storage.
847    */
848   hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
849
850   if (SUCCEEDED(hres))
851   {
852     IStorage_Commit(pStg,
853                     STGC_DEFAULT);
854   }
855
856   return hres;
857 }
858
859
860 /******************************************************************************
861  *              OleLockRunning        [OLE32.114]
862  */
863 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
864 {
865   IRunnableObject* runnable = NULL;
866   HRESULT          hres;
867
868   TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
869
870   hres = IUnknown_QueryInterface(pUnknown,
871                                  &IID_IRunnableObject,
872                                  (void**)&runnable);
873
874   if (SUCCEEDED(hres))
875   {
876     hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
877
878     IRunnableObject_Release(runnable);
879
880     return hres;
881   }
882   else
883     return E_INVALIDARG;
884 }
885
886
887 /**************************************************************************
888  * Internal methods to manage the shared OLE menu in response to the
889  * OLE***MenuDescriptor API
890  */
891
892 /***
893  * OLEMenu_Initialize()
894  *
895  * Initializes the OLEMENU data structures.
896  */
897 static void OLEMenu_Initialize()
898 {
899 }
900
901 /***
902  * OLEMenu_UnInitialize()
903  *
904  * Releases the OLEMENU data structures.
905  */
906 static void OLEMenu_UnInitialize()
907 {
908 }
909
910 /*************************************************************************
911  * OLEMenu_InstallHooks
912  * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
913  *
914  * RETURNS: TRUE if message hooks were succesfully installed
915  *          FALSE on failure
916  */
917 BOOL OLEMenu_InstallHooks( DWORD tid )
918 {
919   OleMenuHookItem *pHookItem = NULL;
920
921   /* Create an entry for the hook table */
922   if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
923                                sizeof(OleMenuHookItem)) ) )
924     return FALSE;
925
926   pHookItem->tid = tid;
927   pHookItem->hHeap = GetProcessHeap();
928
929   /* Install a thread scope message hook for WH_GETMESSAGE */
930   pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
931                                                0, GetCurrentThreadId() );
932   if ( !pHookItem->GetMsg_hHook )
933     goto CLEANUP;
934
935   /* Install a thread scope message hook for WH_CALLWNDPROC */
936   pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
937                                                     0, GetCurrentThreadId() );
938   if ( !pHookItem->CallWndProc_hHook )
939     goto CLEANUP;
940
941   /* Insert the hook table entry */
942   pHookItem->next = hook_list;
943   hook_list = pHookItem;
944
945   return TRUE;
946
947 CLEANUP:
948   /* Unhook any hooks */
949   if ( pHookItem->GetMsg_hHook )
950     UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
951   if ( pHookItem->CallWndProc_hHook )
952     UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
953   /* Release the hook table entry */
954   HeapFree(pHookItem->hHeap, 0, pHookItem );
955
956   return FALSE;
957 }
958
959 /*************************************************************************
960  * OLEMenu_UnInstallHooks
961  * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
962  *
963  * RETURNS: TRUE if message hooks were succesfully installed
964  *          FALSE on failure
965  */
966 BOOL OLEMenu_UnInstallHooks( DWORD tid )
967 {
968   OleMenuHookItem *pHookItem = NULL;
969   OleMenuHookItem **ppHook = &hook_list;
970
971   while (*ppHook)
972   {
973       if ((*ppHook)->tid == tid)
974       {
975           pHookItem = *ppHook;
976           *ppHook = pHookItem->next;
977           break;
978       }
979       ppHook = &(*ppHook)->next;
980   }
981   if (!pHookItem) return FALSE;
982
983   /* Uninstall the hooks installed for this thread */
984   if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
985     goto CLEANUP;
986   if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
987     goto CLEANUP;
988
989   /* Release the hook table entry */
990   HeapFree(pHookItem->hHeap, 0, pHookItem );
991
992   return TRUE;
993
994 CLEANUP:
995   /* Release the hook table entry */
996   if (pHookItem)
997     HeapFree(pHookItem->hHeap, 0, pHookItem );
998
999   return FALSE;
1000 }
1001
1002 /*************************************************************************
1003  * OLEMenu_IsHookInstalled
1004  * Tests if OLEMenu hooks have been installed for a thread
1005  *
1006  * RETURNS: The pointer and index of the hook table entry for the tid
1007  *          NULL and -1 for the index if no hooks were installed for this thread
1008  */
1009 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1010 {
1011   OleMenuHookItem *pHookItem = NULL;
1012
1013   /* Do a simple linear search for an entry whose tid matches ours.
1014    * We really need a map but efficiency is not a concern here. */
1015   for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1016   {
1017     if ( tid == pHookItem->tid )
1018       return pHookItem;
1019   }
1020
1021   return NULL;
1022 }
1023
1024 /***********************************************************************
1025  *           OLEMenu_FindMainMenuIndex
1026  *
1027  * Used by OLEMenu API to find the top level group a menu item belongs to.
1028  * On success pnPos contains the index of the item in the top level menu group
1029  *
1030  * RETURNS: TRUE if the ID was found, FALSE on failure
1031  */
1032 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1033 {
1034   UINT i, nItems;
1035
1036   nItems = GetMenuItemCount( hMainMenu );
1037
1038   for (i = 0; i < nItems; i++)
1039   {
1040     HMENU hsubmenu;
1041
1042     /*  Is the current item a submenu? */
1043     if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1044     {
1045       /* If the handle is the same we're done */
1046       if ( hsubmenu == hPopupMenu )
1047       {
1048         if (pnPos)
1049           *pnPos = i;
1050         return TRUE;
1051       }
1052       /* Recursively search without updating pnPos */
1053       else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1054       {
1055         if (pnPos)
1056           *pnPos = i;
1057         return TRUE;
1058       }
1059     }
1060   }
1061
1062   return FALSE;
1063 }
1064
1065 /***********************************************************************
1066  *           OLEMenu_SetIsServerMenu
1067  *
1068  * Checks whether a popup menu belongs to a shared menu group which is
1069  * owned by the server, and sets the menu descriptor state accordingly.
1070  * All menu messages from these groups should be routed to the server.
1071  *
1072  * RETURNS: TRUE if the popup menu is part of a server owned group
1073  *          FASE if the popup menu is part of a container owned group
1074  */
1075 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1076 {
1077   UINT nPos = 0, nWidth, i;
1078
1079   pOleMenuDescriptor->bIsServerItem = FALSE;
1080
1081   /* Don't bother searching if the popup is the combined menu itself */
1082   if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1083     return FALSE;
1084
1085   /* Find the menu item index in the shared OLE menu that this item belongs to */
1086   if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu,  &nPos ) )
1087     return FALSE;
1088
1089   /* The group widths array has counts for the number of elements
1090    * in the groups File, Edit, Container, Object, Window, Help.
1091    * The Edit, Object & Help groups belong to the server object
1092    * and the other three belong to the container.
1093    * Loop through the group widths and locate the group we are a member of.
1094    */
1095   for ( i = 0, nWidth = 0; i < 6; i++ )
1096   {
1097     nWidth += pOleMenuDescriptor->mgw.width[i];
1098     if ( nPos < nWidth )
1099     {
1100       /* Odd elements are server menu widths */
1101       pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1102       break;
1103     }
1104   }
1105
1106   return pOleMenuDescriptor->bIsServerItem;
1107 }
1108
1109 /*************************************************************************
1110  * OLEMenu_CallWndProc
1111  * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1112  * This is invoked from a message hook installed in OleSetMenuDescriptor.
1113  */
1114 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1115 {
1116   LPCWPSTRUCT pMsg = NULL;
1117   HOLEMENU hOleMenu = 0;
1118   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1119   OleMenuHookItem *pHookItem = NULL;
1120   WORD fuFlags;
1121
1122   TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1123
1124   /* Check if we're being asked to process the message */
1125   if ( HC_ACTION != code )
1126     goto NEXTHOOK;
1127
1128   /* Retrieve the current message being dispatched from lParam */
1129   pMsg = (LPCWPSTRUCT)lParam;
1130
1131   /* Check if the message is destined for a window we are interested in:
1132    * If the window has an OLEMenu property we may need to dispatch
1133    * the menu message to its active objects window instead. */
1134
1135   hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1136   if ( !hOleMenu )
1137     goto NEXTHOOK;
1138
1139   /* Get the menu descriptor */
1140   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1141   if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1142     goto NEXTHOOK;
1143
1144   /* Process menu messages */
1145   switch( pMsg->message )
1146   {
1147     case WM_INITMENU:
1148     {
1149       /* Reset the menu descriptor state */
1150       pOleMenuDescriptor->bIsServerItem = FALSE;
1151
1152       /* Send this message to the server as well */
1153       SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1154                   pMsg->message, pMsg->wParam, pMsg->lParam );
1155       goto NEXTHOOK;
1156     }
1157
1158     case WM_INITMENUPOPUP:
1159     {
1160       /* Save the state for whether this is a server owned menu */
1161       OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1162       break;
1163     }
1164
1165     case WM_MENUSELECT:
1166     {
1167       fuFlags = HIWORD(pMsg->wParam);  /* Get flags */
1168       if ( fuFlags & MF_SYSMENU )
1169          goto NEXTHOOK;
1170
1171       /* Save the state for whether this is a server owned popup menu */
1172       else if ( fuFlags & MF_POPUP )
1173         OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1174
1175       break;
1176     }
1177
1178     case WM_DRAWITEM:
1179     {
1180       LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1181       if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1182         goto NEXTHOOK;  /* Not a menu message */
1183
1184       break;
1185     }
1186
1187     default:
1188       goto NEXTHOOK;
1189   }
1190
1191   /* If the message was for the server dispatch it accordingly */
1192   if ( pOleMenuDescriptor->bIsServerItem )
1193   {
1194     SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1195                   pMsg->message, pMsg->wParam, pMsg->lParam );
1196   }
1197
1198 NEXTHOOK:
1199   if ( pOleMenuDescriptor )
1200     GlobalUnlock( hOleMenu );
1201
1202   /* Lookup the hook item for the current thread */
1203   if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1204   {
1205     /* This should never fail!! */
1206     WARN("could not retrieve hHook for current thread!\n" );
1207     return 0;
1208   }
1209
1210   /* Pass on the message to the next hooker */
1211   return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1212 }
1213
1214 /*************************************************************************
1215  * OLEMenu_GetMsgProc
1216  * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1217  * This is invoked from a message hook installed in OleSetMenuDescriptor.
1218  */
1219 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1220 {
1221   LPMSG pMsg = NULL;
1222   HOLEMENU hOleMenu = 0;
1223   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1224   OleMenuHookItem *pHookItem = NULL;
1225   WORD wCode;
1226
1227   TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1228
1229   /* Check if we're being asked to process a  messages */
1230   if ( HC_ACTION != code )
1231     goto NEXTHOOK;
1232
1233   /* Retrieve the current message being dispatched from lParam */
1234   pMsg = (LPMSG)lParam;
1235
1236   /* Check if the message is destined for a window we are interested in:
1237    * If the window has an OLEMenu property we may need to dispatch
1238    * the menu message to its active objects window instead. */
1239
1240   hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1241   if ( !hOleMenu )
1242     goto NEXTHOOK;
1243
1244   /* Process menu messages */
1245   switch( pMsg->message )
1246   {
1247     case WM_COMMAND:
1248     {
1249       wCode = HIWORD(pMsg->wParam);  /* Get notification code */
1250       if ( wCode )
1251         goto NEXTHOOK;  /* Not a menu message */
1252       break;
1253     }
1254     default:
1255       goto NEXTHOOK;
1256   }
1257
1258   /* Get the menu descriptor */
1259   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1260   if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1261     goto NEXTHOOK;
1262
1263   /* If the message was for the server dispatch it accordingly */
1264   if ( pOleMenuDescriptor->bIsServerItem )
1265   {
1266     /* Change the hWnd in the message to the active objects hWnd.
1267      * The message loop which reads this message will automatically
1268      * dispatch it to the embedded objects window. */
1269     pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1270   }
1271
1272 NEXTHOOK:
1273   if ( pOleMenuDescriptor )
1274     GlobalUnlock( hOleMenu );
1275
1276   /* Lookup the hook item for the current thread */
1277   if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1278   {
1279     /* This should never fail!! */
1280     WARN("could not retrieve hHook for current thread!\n" );
1281     return FALSE;
1282   }
1283
1284   /* Pass on the message to the next hooker */
1285   return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1286 }
1287
1288 /***********************************************************************
1289  * OleCreateMenuDescriptor [OLE32.97]
1290  * Creates an OLE menu descriptor for OLE to use when dispatching
1291  * menu messages and commands.
1292  *
1293  * PARAMS:
1294  *    hmenuCombined  -  Handle to the objects combined menu
1295  *    lpMenuWidths   -  Pointer to array of 6 LONG's indicating menus per group
1296  *
1297  */
1298 HOLEMENU WINAPI OleCreateMenuDescriptor(
1299   HMENU                hmenuCombined,
1300   LPOLEMENUGROUPWIDTHS lpMenuWidths)
1301 {
1302   HOLEMENU hOleMenu;
1303   OleMenuDescriptor *pOleMenuDescriptor;
1304   int i;
1305
1306   if ( !hmenuCombined || !lpMenuWidths )
1307     return 0;
1308
1309   /* Create an OLE menu descriptor */
1310   if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1311                                 sizeof(OleMenuDescriptor) ) ) )
1312   return 0;
1313
1314   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1315   if ( !pOleMenuDescriptor )
1316     return 0;
1317
1318   /* Initialize menu group widths and hmenu */
1319   for ( i = 0; i < 6; i++ )
1320     pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1321
1322   pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1323   pOleMenuDescriptor->bIsServerItem = FALSE;
1324   GlobalUnlock( hOleMenu );
1325
1326   return hOleMenu;
1327 }
1328
1329 /***********************************************************************
1330  * OleDestroyMenuDescriptor [OLE32.99]
1331  * Destroy the shared menu descriptor
1332  */
1333 HRESULT WINAPI OleDestroyMenuDescriptor(
1334   HOLEMENU hmenuDescriptor)
1335 {
1336   if ( hmenuDescriptor )
1337     GlobalFree( hmenuDescriptor );
1338         return S_OK;
1339 }
1340
1341 /***********************************************************************
1342  * OleSetMenuDescriptor [OLE32.129]
1343  * Installs or removes OLE dispatching code for the containers frame window
1344  * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1345  * OLE should install context sensitive help F1 filtering for the app when
1346  * these are non null.
1347  *
1348  * PARAMS:
1349  *     hOleMenu         Handle to composite menu descriptor
1350  *     hwndFrame        Handle to containers frame window
1351  *     hwndActiveObject Handle to objects in-place activation window
1352  *     lpFrame          Pointer to IOleInPlaceFrame on containers window
1353  *     lpActiveObject   Pointer to IOleInPlaceActiveObject on active in-place object
1354  *
1355  * RETURNS:
1356  *      S_OK                               - menu installed correctly
1357  *      E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1358  */
1359 HRESULT WINAPI OleSetMenuDescriptor(
1360   HOLEMENU               hOleMenu,
1361   HWND                   hwndFrame,
1362   HWND                   hwndActiveObject,
1363   LPOLEINPLACEFRAME        lpFrame,
1364   LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1365 {
1366   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1367
1368   /* Check args */
1369   if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1370     return E_INVALIDARG;
1371
1372   if ( lpFrame || lpActiveObject )
1373   {
1374      FIXME("(%x, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1375         (unsigned int)hOleMenu,
1376         hwndFrame,
1377         hwndActiveObject,
1378         lpFrame,
1379         lpActiveObject);
1380   }
1381
1382   /* Set up a message hook to intercept the containers frame window messages.
1383    * The message filter is responsible for dispatching menu messages from the
1384    * shared menu which are intended for the object.
1385    */
1386
1387   if ( hOleMenu )  /* Want to install dispatching code */
1388   {
1389     /* If OLEMenu hooks are already installed for this thread, fail
1390      * Note: This effectively means that OleSetMenuDescriptor cannot
1391      * be called twice in succession on the same frame window
1392      * without first calling it with a null hOleMenu to uninstall */
1393     if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1394   return E_FAIL;
1395
1396     /* Get the menu descriptor */
1397     pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1398     if ( !pOleMenuDescriptor )
1399       return E_UNEXPECTED;
1400
1401     /* Update the menu descriptor */
1402     pOleMenuDescriptor->hwndFrame = hwndFrame;
1403     pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1404
1405     GlobalUnlock( hOleMenu );
1406     pOleMenuDescriptor = NULL;
1407
1408     /* Add a menu descriptor windows property to the frame window */
1409     SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1410
1411     /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1412     if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1413       return E_FAIL;
1414   }
1415   else  /* Want to uninstall dispatching code */
1416   {
1417     /* Uninstall the hooks */
1418     if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1419       return E_FAIL;
1420
1421     /* Remove the menu descriptor property from the frame window */
1422     RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1423   }
1424
1425   return S_OK;
1426 }
1427
1428 /******************************************************************************
1429  *              IsAccelerator        [OLE32.75]
1430  * Mostly copied from controls/menu.c TranslateAccelerator implementation
1431  */
1432 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1433 {
1434     /* YES, Accel16! */
1435     LPACCEL16 lpAccelTbl;
1436     int i;
1437
1438     if(!lpMsg) return FALSE;
1439     if (!hAccel || !(lpAccelTbl = (LPACCEL16)LockResource16(HACCEL_16(hAccel))))
1440     {
1441         WARN_(accel)("invalid accel handle=%p\n", hAccel);
1442         return FALSE;
1443     }
1444     if((lpMsg->message != WM_KEYDOWN &&
1445         lpMsg->message != WM_KEYUP &&
1446         lpMsg->message != WM_SYSKEYDOWN &&
1447         lpMsg->message != WM_SYSKEYUP &&
1448         lpMsg->message != WM_CHAR)) return FALSE;
1449
1450     TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1451                 "msg->hwnd=%p, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1452                 hAccel, cAccelEntries,
1453                 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1454     for(i = 0; i < cAccelEntries; i++)
1455     {
1456         if(lpAccelTbl[i].key != lpMsg->wParam)
1457             continue;
1458
1459         if(lpMsg->message == WM_CHAR)
1460         {
1461             if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1462             {
1463                 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1464                 goto found;
1465             }
1466         }
1467         else
1468         {
1469             if(lpAccelTbl[i].fVirt & FVIRTKEY)
1470             {
1471                 INT mask = 0;
1472                 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1473                                 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1474                 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1475                 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1476                 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1477                 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1478                 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1479             }
1480             else
1481             {
1482                 if(!(lpMsg->lParam & 0x01000000))  /* no special_key */
1483                 {
1484                     if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1485                     {                                                  /* ^^ ALT pressed */
1486                         TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1487                         goto found;
1488                     }
1489                 }
1490             }
1491         }
1492     }
1493
1494     WARN_(accel)("couldn't translate accelerator key\n");
1495     return FALSE;
1496
1497 found:
1498     if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1499     return TRUE;
1500 }
1501
1502 /***********************************************************************
1503  * ReleaseStgMedium [OLE32.140]
1504  */
1505 void WINAPI ReleaseStgMedium(
1506   STGMEDIUM* pmedium)
1507 {
1508   switch (pmedium->tymed)
1509   {
1510     case TYMED_HGLOBAL:
1511     {
1512       if ( (pmedium->pUnkForRelease==0) &&
1513            (pmedium->u.hGlobal!=0) )
1514         GlobalFree(pmedium->u.hGlobal);
1515
1516       pmedium->u.hGlobal = 0;
1517       break;
1518     }
1519     case TYMED_FILE:
1520     {
1521       if (pmedium->u.lpszFileName!=0)
1522       {
1523         if (pmedium->pUnkForRelease==0)
1524         {
1525           DeleteFileW(pmedium->u.lpszFileName);
1526         }
1527
1528         CoTaskMemFree(pmedium->u.lpszFileName);
1529       }
1530
1531       pmedium->u.lpszFileName = 0;
1532       break;
1533     }
1534     case TYMED_ISTREAM:
1535     {
1536       if (pmedium->u.pstm!=0)
1537       {
1538         IStream_Release(pmedium->u.pstm);
1539       }
1540
1541       pmedium->u.pstm = 0;
1542       break;
1543     }
1544     case TYMED_ISTORAGE:
1545     {
1546       if (pmedium->u.pstg!=0)
1547       {
1548         IStorage_Release(pmedium->u.pstg);
1549       }
1550
1551       pmedium->u.pstg = 0;
1552       break;
1553     }
1554     case TYMED_GDI:
1555     {
1556       if ( (pmedium->pUnkForRelease==0) &&
1557            (pmedium->u.hGlobal!=0) )
1558         DeleteObject(pmedium->u.hGlobal);
1559
1560       pmedium->u.hGlobal = 0;
1561       break;
1562     }
1563     case TYMED_MFPICT:
1564     {
1565       if ( (pmedium->pUnkForRelease==0) &&
1566            (pmedium->u.hMetaFilePict!=0) )
1567       {
1568         LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hGlobal);
1569         DeleteMetaFile(pMP->hMF);
1570         GlobalUnlock(pmedium->u.hGlobal);
1571         GlobalFree(pmedium->u.hGlobal);
1572       }
1573
1574       pmedium->u.hMetaFilePict = 0;
1575       break;
1576     }
1577     case TYMED_ENHMF:
1578     {
1579       if ( (pmedium->pUnkForRelease==0) &&
1580            (pmedium->u.hEnhMetaFile!=0) )
1581       {
1582         DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1583       }
1584
1585       pmedium->u.hEnhMetaFile = 0;
1586       break;
1587     }
1588     case TYMED_NULL:
1589     default:
1590       break;
1591   }
1592
1593   /*
1594    * After cleaning up, the unknown is released
1595    */
1596   if (pmedium->pUnkForRelease!=0)
1597   {
1598     IUnknown_Release(pmedium->pUnkForRelease);
1599     pmedium->pUnkForRelease = 0;
1600   }
1601 }
1602
1603 /***
1604  * OLEDD_Initialize()
1605  *
1606  * Initializes the OLE drag and drop data structures.
1607  */
1608 static void OLEDD_Initialize()
1609 {
1610     WNDCLASSA wndClass;
1611
1612     ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1613     wndClass.style         = CS_GLOBALCLASS;
1614     wndClass.lpfnWndProc   = (WNDPROC)OLEDD_DragTrackerWindowProc;
1615     wndClass.cbClsExtra    = 0;
1616     wndClass.cbWndExtra    = sizeof(TrackerWindowInfo*);
1617     wndClass.hCursor       = 0;
1618     wndClass.hbrBackground = 0;
1619     wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1620
1621     RegisterClassA (&wndClass);
1622 }
1623
1624 /***
1625  * OLEDD_UnInitialize()
1626  *
1627  * Releases the OLE drag and drop data structures.
1628  */
1629 static void OLEDD_UnInitialize()
1630 {
1631   /*
1632    * Simply empty the list.
1633    */
1634   while (targetListHead!=NULL)
1635   {
1636     RevokeDragDrop(targetListHead->hwndTarget);
1637   }
1638 }
1639
1640 /***
1641  * OLEDD_InsertDropTarget()
1642  *
1643  * Insert the target node in the tree.
1644  */
1645 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1646 {
1647   DropTargetNode*  curNode;
1648   DropTargetNode** parentNodeLink;
1649
1650   /*
1651    * Iterate the tree to find the insertion point.
1652    */
1653   curNode        = targetListHead;
1654   parentNodeLink = &targetListHead;
1655
1656   while (curNode!=NULL)
1657   {
1658     if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1659     {
1660       /*
1661        * If the node we want to add has a smaller HWND, go left
1662        */
1663       parentNodeLink = &curNode->prevDropTarget;
1664       curNode        =  curNode->prevDropTarget;
1665     }
1666     else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1667     {
1668       /*
1669        * If the node we want to add has a larger HWND, go right
1670        */
1671       parentNodeLink = &curNode->nextDropTarget;
1672       curNode        =  curNode->nextDropTarget;
1673     }
1674     else
1675     {
1676       /*
1677        * The item was found in the list. It shouldn't have been there
1678        */
1679       assert(FALSE);
1680       return;
1681     }
1682   }
1683
1684   /*
1685    * If we get here, we have found a spot for our item. The parentNodeLink
1686    * pointer points to the pointer that we have to modify.
1687    * The curNode should be NULL. We just have to establish the link and Voila!
1688    */
1689   assert(curNode==NULL);
1690   assert(parentNodeLink!=NULL);
1691   assert(*parentNodeLink==NULL);
1692
1693   *parentNodeLink=nodeToAdd;
1694 }
1695
1696 /***
1697  * OLEDD_ExtractDropTarget()
1698  *
1699  * Removes the target node from the tree.
1700  */
1701 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1702 {
1703   DropTargetNode*  curNode;
1704   DropTargetNode** parentNodeLink;
1705
1706   /*
1707    * Iterate the tree to find the insertion point.
1708    */
1709   curNode        = targetListHead;
1710   parentNodeLink = &targetListHead;
1711
1712   while (curNode!=NULL)
1713   {
1714     if (hwndOfTarget<curNode->hwndTarget)
1715     {
1716       /*
1717        * If the node we want to add has a smaller HWND, go left
1718        */
1719       parentNodeLink = &curNode->prevDropTarget;
1720       curNode        =  curNode->prevDropTarget;
1721     }
1722     else if (hwndOfTarget>curNode->hwndTarget)
1723     {
1724       /*
1725        * If the node we want to add has a larger HWND, go right
1726        */
1727       parentNodeLink = &curNode->nextDropTarget;
1728       curNode        =  curNode->nextDropTarget;
1729     }
1730     else
1731     {
1732       /*
1733        * The item was found in the list. Detach it from it's parent and
1734        * re-insert it's kids in the tree.
1735        */
1736       assert(parentNodeLink!=NULL);
1737       assert(*parentNodeLink==curNode);
1738
1739       /*
1740        * We arbitrately re-attach the left sub-tree to the parent.
1741        */
1742       *parentNodeLink = curNode->prevDropTarget;
1743
1744       /*
1745        * And we re-insert the right subtree
1746        */
1747       if (curNode->nextDropTarget!=NULL)
1748       {
1749         OLEDD_InsertDropTarget(curNode->nextDropTarget);
1750       }
1751
1752       /*
1753        * The node we found is still a valid node once we complete
1754        * the unlinking of the kids.
1755        */
1756       curNode->nextDropTarget=NULL;
1757       curNode->prevDropTarget=NULL;
1758
1759       return curNode;
1760     }
1761   }
1762
1763   /*
1764    * If we get here, the node is not in the tree
1765    */
1766   return NULL;
1767 }
1768
1769 /***
1770  * OLEDD_FindDropTarget()
1771  *
1772  * Finds information about the drop target.
1773  */
1774 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1775 {
1776   DropTargetNode*  curNode;
1777
1778   /*
1779    * Iterate the tree to find the HWND value.
1780    */
1781   curNode        = targetListHead;
1782
1783   while (curNode!=NULL)
1784   {
1785     if (hwndOfTarget<curNode->hwndTarget)
1786     {
1787       /*
1788        * If the node we want to add has a smaller HWND, go left
1789        */
1790       curNode =  curNode->prevDropTarget;
1791     }
1792     else if (hwndOfTarget>curNode->hwndTarget)
1793     {
1794       /*
1795        * If the node we want to add has a larger HWND, go right
1796        */
1797       curNode =  curNode->nextDropTarget;
1798     }
1799     else
1800     {
1801       /*
1802        * The item was found in the list.
1803        */
1804       return curNode;
1805     }
1806   }
1807
1808   /*
1809    * If we get here, the item is not in the list
1810    */
1811   return NULL;
1812 }
1813
1814 /***
1815  * OLEDD_DragTrackerWindowProc()
1816  *
1817  * This method is the WindowProcedure of the drag n drop tracking
1818  * window. During a drag n Drop operation, an invisible window is created
1819  * to receive the user input and act upon it. This procedure is in charge
1820  * of this behavior.
1821  */
1822 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1823                          HWND   hwnd,
1824                          UINT   uMsg,
1825                          WPARAM wParam,
1826                          LPARAM   lParam)
1827 {
1828   switch (uMsg)
1829   {
1830     case WM_CREATE:
1831     {
1832       LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1833
1834       SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1835
1836
1837       break;
1838     }
1839     case WM_MOUSEMOVE:
1840     {
1841       TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1842       POINT            mousePos;
1843
1844       /*
1845        * Get the current mouse position in screen coordinates.
1846        */
1847       mousePos.x = LOWORD(lParam);
1848       mousePos.y = HIWORD(lParam);
1849       ClientToScreen(hwnd, &mousePos);
1850
1851       /*
1852        * Track the movement of the mouse.
1853        */
1854       OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1855
1856       break;
1857     }
1858     case WM_LBUTTONUP:
1859     case WM_MBUTTONUP:
1860     case WM_RBUTTONUP:
1861     case WM_LBUTTONDOWN:
1862     case WM_MBUTTONDOWN:
1863     case WM_RBUTTONDOWN:
1864     {
1865       TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1866       POINT            mousePos;
1867
1868       /*
1869        * Get the current mouse position in screen coordinates.
1870        */
1871       mousePos.x = LOWORD(lParam);
1872       mousePos.y = HIWORD(lParam);
1873       ClientToScreen(hwnd, &mousePos);
1874
1875       /*
1876        * Notify everyone that the button state changed
1877        * TODO: Check if the "escape" key was pressed.
1878        */
1879       OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1880
1881       break;
1882     }
1883   }
1884
1885   /*
1886    * This is a window proc after all. Let's call the default.
1887    */
1888   return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1889 }
1890
1891 /***
1892  * OLEDD_TrackMouseMove()
1893  *
1894  * This method is invoked while a drag and drop operation is in effect.
1895  * it will generate the appropriate callbacks in the drop source
1896  * and drop target. It will also provide the expected feedback to
1897  * the user.
1898  *
1899  * params:
1900  *    trackerInfo - Pointer to the structure identifying the
1901  *                  drag & drop operation that is currently
1902  *                  active.
1903  *    mousePos    - Current position of the mouse in screen
1904  *                  coordinates.
1905  *    keyState    - Contains the state of the shift keys and the
1906  *                  mouse buttons (MK_LBUTTON and the like)
1907  */
1908 static void OLEDD_TrackMouseMove(
1909   TrackerWindowInfo* trackerInfo,
1910   POINT            mousePos,
1911   DWORD              keyState)
1912 {
1913   HWND   hwndNewTarget = 0;
1914   HRESULT  hr = S_OK;
1915
1916   /*
1917    * Get the handle of the window under the mouse
1918    */
1919   hwndNewTarget = WindowFromPoint(mousePos);
1920
1921   /*
1922    * Every time, we re-initialize the effects passed to the
1923    * IDropTarget to the effects allowed by the source.
1924    */
1925   *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1926
1927   /*
1928    * If we are hovering over the same target as before, send the
1929    * DragOver notification
1930    */
1931   if ( (trackerInfo->curDragTarget != 0) &&
1932        (trackerInfo->curDragTargetHWND==hwndNewTarget) )
1933   {
1934     POINTL  mousePosParam;
1935
1936     /*
1937      * The documentation tells me that the coordinate should be in the target
1938      * window's coordinate space. However, the tests I made tell me the
1939      * coordinates should be in screen coordinates.
1940      */
1941     mousePosParam.x = mousePos.x;
1942     mousePosParam.y = mousePos.y;
1943
1944     IDropTarget_DragOver(trackerInfo->curDragTarget,
1945                          keyState,
1946                          mousePosParam,
1947                          trackerInfo->pdwEffect);
1948   }
1949   else
1950   {
1951     DropTargetNode* newDropTargetNode = 0;
1952
1953     /*
1954      * If we changed window, we have to notify our old target and check for
1955      * the new one.
1956      */
1957     if (trackerInfo->curDragTarget!=0)
1958     {
1959       IDropTarget_DragLeave(trackerInfo->curDragTarget);
1960     }
1961
1962     /*
1963      * Make sure we're hovering over a window.
1964      */
1965     if (hwndNewTarget!=0)
1966     {
1967       /*
1968        * Find-out if there is a drag target under the mouse
1969        */
1970       HWND nexttar = hwndNewTarget;
1971       do {
1972         newDropTargetNode = OLEDD_FindDropTarget(nexttar);
1973       } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
1974       if(nexttar) hwndNewTarget = nexttar;
1975
1976       trackerInfo->curDragTargetHWND = hwndNewTarget;
1977       trackerInfo->curDragTarget     = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1978
1979       /*
1980        * If there is, notify it that we just dragged-in
1981        */
1982       if (trackerInfo->curDragTarget!=0)
1983       {
1984         POINTL  mousePosParam;
1985
1986         /*
1987          * The documentation tells me that the coordinate should be in the target
1988          * window's coordinate space. However, the tests I made tell me the
1989          * coordinates should be in screen coordinates.
1990          */
1991         mousePosParam.x = mousePos.x;
1992         mousePosParam.y = mousePos.y;
1993
1994         IDropTarget_DragEnter(trackerInfo->curDragTarget,
1995                               trackerInfo->dataObject,
1996                               keyState,
1997                               mousePosParam,
1998                               trackerInfo->pdwEffect);
1999       }
2000     }
2001     else
2002     {
2003       /*
2004        * The mouse is not over a window so we don't track anything.
2005        */
2006       trackerInfo->curDragTargetHWND = 0;
2007       trackerInfo->curDragTarget     = 0;
2008     }
2009   }
2010
2011   /*
2012    * Now that we have done that, we have to tell the source to give
2013    * us feedback on the work being done by the target.  If we don't
2014    * have a target, simulate no effect.
2015    */
2016   if (trackerInfo->curDragTarget==0)
2017   {
2018     *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2019   }
2020
2021   hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2022                                 *trackerInfo->pdwEffect);
2023
2024   /*
2025    * When we ask for feedback from the drop source, sometimes it will
2026    * do all the necessary work and sometimes it will not handle it
2027    * when that's the case, we must display the standard drag and drop
2028    * cursors.
2029    */
2030   if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2031   {
2032     if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2033     {
2034       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2035     }
2036     else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2037     {
2038       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2039     }
2040     else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2041     {
2042       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2043     }
2044     else
2045     {
2046       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2047     }
2048   }
2049 }
2050
2051 /***
2052  * OLEDD_TrackStateChange()
2053  *
2054  * This method is invoked while a drag and drop operation is in effect.
2055  * It is used to notify the drop target/drop source callbacks when
2056  * the state of the keyboard or mouse button change.
2057  *
2058  * params:
2059  *    trackerInfo - Pointer to the structure identifying the
2060  *                  drag & drop operation that is currently
2061  *                  active.
2062  *    mousePos    - Current position of the mouse in screen
2063  *                  coordinates.
2064  *    keyState    - Contains the state of the shift keys and the
2065  *                  mouse buttons (MK_LBUTTON and the like)
2066  */
2067 static void OLEDD_TrackStateChange(
2068   TrackerWindowInfo* trackerInfo,
2069   POINT            mousePos,
2070   DWORD              keyState)
2071 {
2072   /*
2073    * Ask the drop source what to do with the operation.
2074    */
2075   trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2076                                trackerInfo->dropSource,
2077                                trackerInfo->escPressed,
2078                                keyState);
2079
2080   /*
2081    * All the return valued will stop the operation except the S_OK
2082    * return value.
2083    */
2084   if (trackerInfo->returnValue!=S_OK)
2085   {
2086     /*
2087      * Make sure the message loop in DoDragDrop stops
2088      */
2089     trackerInfo->trackingDone = TRUE;
2090
2091     /*
2092      * Release the mouse in case the drop target decides to show a popup
2093      * or a menu or something.
2094      */
2095     ReleaseCapture();
2096
2097     /*
2098      * If we end-up over a target, drop the object in the target or
2099      * inform the target that the operation was cancelled.
2100      */
2101     if (trackerInfo->curDragTarget!=0)
2102     {
2103       switch (trackerInfo->returnValue)
2104       {
2105         /*
2106          * If the source wants us to complete the operation, we tell
2107          * the drop target that we just dropped the object in it.
2108          */
2109         case DRAGDROP_S_DROP:
2110         {
2111           POINTL  mousePosParam;
2112
2113           /*
2114            * The documentation tells me that the coordinate should be
2115            * in the target window's coordinate space. However, the tests
2116            * I made tell me the coordinates should be in screen coordinates.
2117            */
2118           mousePosParam.x = mousePos.x;
2119           mousePosParam.y = mousePos.y;
2120
2121           IDropTarget_Drop(trackerInfo->curDragTarget,
2122                            trackerInfo->dataObject,
2123                            keyState,
2124                            mousePosParam,
2125                            trackerInfo->pdwEffect);
2126           break;
2127         }
2128         /*
2129          * If the source told us that we should cancel, fool the drop
2130          * target by telling it that the mouse left it's window.
2131          * Also set the drop effect to "NONE" in case the application
2132          * ignores the result of DoDragDrop.
2133          */
2134         case DRAGDROP_S_CANCEL:
2135           IDropTarget_DragLeave(trackerInfo->curDragTarget);
2136           *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2137           break;
2138       }
2139     }
2140   }
2141 }
2142
2143 /***
2144  * OLEDD_GetButtonState()
2145  *
2146  * This method will use the current state of the keyboard to build
2147  * a button state mask equivalent to the one passed in the
2148  * WM_MOUSEMOVE wParam.
2149  */
2150 static DWORD OLEDD_GetButtonState()
2151 {
2152   BYTE  keyboardState[256];
2153   DWORD keyMask = 0;
2154
2155   GetKeyboardState(keyboardState);
2156
2157   if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2158     keyMask |= MK_SHIFT;
2159
2160   if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2161     keyMask |= MK_CONTROL;
2162
2163   if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2164     keyMask |= MK_LBUTTON;
2165
2166   if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2167     keyMask |= MK_RBUTTON;
2168
2169   if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2170     keyMask |= MK_MBUTTON;
2171
2172   return keyMask;
2173 }
2174
2175 /***
2176  * OLEDD_GetButtonState()
2177  *
2178  * This method will read the default value of the registry key in
2179  * parameter and extract a DWORD value from it. The registry key value
2180  * can be in a string key or a DWORD key.
2181  *
2182  * params:
2183  *     regKey   - Key to read the default value from
2184  *     pdwValue - Pointer to the location where the DWORD
2185  *                value is returned. This value is not modified
2186  *                if the value is not found.
2187  */
2188
2189 static void OLEUTL_ReadRegistryDWORDValue(
2190   HKEY   regKey,
2191   DWORD* pdwValue)
2192 {
2193   char  buffer[20];
2194   DWORD dwKeyType;
2195   DWORD cbData = 20;
2196   LONG  lres;
2197
2198   lres = RegQueryValueExA(regKey,
2199                           "",
2200                           NULL,
2201                           &dwKeyType,
2202                           (LPBYTE)buffer,
2203                           &cbData);
2204
2205   if (lres==ERROR_SUCCESS)
2206   {
2207     switch (dwKeyType)
2208     {
2209       case REG_DWORD:
2210         *pdwValue = *(DWORD*)buffer;
2211         break;
2212       case REG_EXPAND_SZ:
2213       case REG_MULTI_SZ:
2214       case REG_SZ:
2215         *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2216         break;
2217     }
2218   }
2219 }
2220
2221 /******************************************************************************
2222  * OleMetaFilePictFromIconAndLabel (OLE2.56)
2223  *
2224  * Returns a global memory handle to a metafile which contains the icon and
2225  * label given.
2226  * I guess the result of that should look somehow like desktop icons.
2227  * If no hIcon is given, we load the icon via lpszSourceFile and iIconIndex.
2228  * This code might be wrong at some places.
2229  */
2230 HGLOBAL16 WINAPI OleMetaFilePictFromIconAndLabel16(
2231         HICON16 hIcon,
2232         LPCOLESTR16 lpszLabel,
2233         LPCOLESTR16 lpszSourceFile,
2234         UINT16 iIconIndex
2235 ) {
2236     METAFILEPICT16 *mf;
2237     HGLOBAL16 hmf;
2238     HDC hdc;
2239
2240     FIXME("(%04x, '%s', '%s', %d): incorrect metrics, please try to correct them !\n\n\n", hIcon, lpszLabel, lpszSourceFile, iIconIndex);
2241
2242     if (!hIcon) {
2243         if (lpszSourceFile) {
2244             HINSTANCE16 hInstance = LoadLibrary16(lpszSourceFile);
2245
2246             /* load the icon at index from lpszSourceFile */
2247             hIcon = HICON_16(LoadIconA(HINSTANCE_32(hInstance), (LPCSTR)(DWORD)iIconIndex));
2248             FreeLibrary16(hInstance);
2249         } else
2250             return 0;
2251     }
2252
2253     hdc = CreateMetaFileA(NULL);
2254     DrawIcon(hdc, 0, 0, HICON_32(hIcon)); /* FIXME */
2255     TextOutA(hdc, 0, 0, lpszLabel, 1); /* FIXME */
2256     hmf = GlobalAlloc16(0, sizeof(METAFILEPICT16));
2257     mf = (METAFILEPICT16 *)GlobalLock16(hmf);
2258     mf->mm = MM_ANISOTROPIC;
2259     mf->xExt = 20; /* FIXME: bogus */
2260     mf->yExt = 20; /* dito */
2261     mf->hMF = CloseMetaFile16(HDC_16(hdc));
2262     return hmf;
2263 }
2264
2265 /******************************************************************************
2266  * DllDebugObjectRPCHook (OLE32.62)
2267  * turns on and off internal debugging,  pointer is only used on macintosh
2268  */
2269
2270 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2271 {
2272   FIXME("stub\n");
2273   return TRUE;
2274 }