- implementation of RtlReg* (read access), RtlEvent*, RtlSemaphore*,
[wine] / dlls / shell32 / contmenu.c
1 /*
2  *      IContextMenu
3  *
4  *      Copyright 1998  Juergen Schmied <juergen.schmied@metronet.de>
5  */
6 #include <string.h>
7
8 #include "winerror.h"
9 #include "debugtools.h"
10
11 #include "pidl.h"
12 #include "wine/obj_base.h"
13 #include "wine/obj_contextmenu.h"
14 #include "wine/obj_shellbrowser.h"
15 #include "wine/obj_shellextinit.h"
16 #include "wine/undocshell.h"
17
18 #include "shell32_main.h"
19
20 DEFAULT_DEBUG_CHANNEL(shell)
21
22 /**************************************************************************
23 * IContextMenu VTable
24
25 */
26
27 static HRESULT WINAPI IContextMenu_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj);
28 static ULONG WINAPI IContextMenu_fnAddRef(IContextMenu *iface);
29 static ULONG WINAPI IContextMenu_fnRelease(IContextMenu *iface);
30 static HRESULT WINAPI IContextMenu_fnQueryContextMenu(IContextMenu *iface, HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags);
31 static HRESULT WINAPI IContextMenu_fnInvokeCommand(IContextMenu *iface, LPCMINVOKECOMMANDINFO lpcmi);
32 static HRESULT WINAPI IContextMenu_fnGetCommandString(IContextMenu *iface, UINT idCommand, UINT uFlags, LPUINT lpReserved, LPSTR lpszName, UINT uMaxNameLen);
33 static HRESULT WINAPI IContextMenu_fnHandleMenuMsg(IContextMenu *iface, UINT uMsg, WPARAM wParam, LPARAM lParam);
34
35 static struct ICOM_VTABLE(IContextMenu) cmvt = 
36 {       
37         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
38         IContextMenu_fnQueryInterface,
39         IContextMenu_fnAddRef,
40         IContextMenu_fnRelease,
41         IContextMenu_fnQueryContextMenu,
42         IContextMenu_fnInvokeCommand,
43         IContextMenu_fnGetCommandString,
44         IContextMenu_fnHandleMenuMsg,
45         (void *) 0xdeadbabe     /* just paranoia */
46 };
47
48 /**************************************************************************
49 *  IContextMenu Implementation
50 */
51 typedef struct 
52 {       ICOM_VFIELD(IContextMenu);
53         DWORD           ref;
54         IShellFolder*   pSFParent;
55         LPITEMIDLIST    pidl;           /* root pidl */
56         LPITEMIDLIST    *aPidls;        /* array of child pidls */
57         BOOL            bAllValues;
58 } IContextMenuImpl;
59
60
61 static struct ICOM_VTABLE(IContextMenu) cmvt;
62
63 /**************************************************************************
64 *  IContextMenu_AllocPidlTable()
65 */
66 static BOOL IContextMenu_AllocPidlTable(IContextMenuImpl *This, DWORD dwEntries)
67 {
68         TRACE("(%p)->(entrys=%lu)\n",This, dwEntries);
69
70         /*add one for NULL terminator */
71         dwEntries++;
72
73         This->aPidls = (LPITEMIDLIST*)SHAlloc(dwEntries * sizeof(LPITEMIDLIST));
74
75         if(This->aPidls)
76         { ZeroMemory(This->aPidls, dwEntries * sizeof(LPITEMIDLIST));   /*set all of the entries to NULL*/
77         }
78         return (This->aPidls != NULL);
79 }
80
81 /**************************************************************************
82 * IContextMenu_FreePidlTable()
83 */
84 static void IContextMenu_FreePidlTable(IContextMenuImpl *This)
85 {
86         int   i;
87
88         TRACE("(%p)->()\n",This);
89
90         if(This->aPidls)
91         { for(i = 0; This->aPidls[i]; i++)
92           { SHFree(This->aPidls[i]);
93           }
94
95           SHFree(This->aPidls);
96           This->aPidls = NULL;
97         }
98 }
99
100 /**************************************************************************
101 * IContextMenu_FillPidlTable()
102 */
103 static BOOL IContextMenu_FillPidlTable(IContextMenuImpl *This, LPCITEMIDLIST *aPidls, UINT uItemCount)
104 {
105         UINT  i;
106
107         TRACE("(%p)->(apidl=%p count=%u)\n",This, aPidls, uItemCount);
108
109         if(This->aPidls)
110         { for(i = 0; i < uItemCount; i++)
111           { This->aPidls[i] = ILClone(aPidls[i]);
112           }
113           return TRUE;
114         }
115         return FALSE;
116 }
117
118 /**************************************************************************
119 * IContextMenu_CanRenameItems()
120 */
121 static BOOL IContextMenu_CanRenameItems(IContextMenuImpl *This)
122 {       UINT  i;
123         DWORD dwAttributes;
124
125         TRACE("(%p)->()\n",This);
126
127         if(This->aPidls)
128         {
129           for(i = 0; This->aPidls[i]; i++){} /*get the number of items assigned to This object*/
130           { if(i > 1)   /*you can't rename more than one item at a time*/
131             { return FALSE;
132             }
133           }
134           dwAttributes = SFGAO_CANRENAME;
135           IShellFolder_GetAttributesOf(This->pSFParent, i, (LPCITEMIDLIST*)This->aPidls, &dwAttributes);
136
137           return dwAttributes & SFGAO_CANRENAME;
138         }
139         return FALSE;
140 }
141
142 /**************************************************************************
143 *   IContextMenu_Constructor()
144 */
145 IContextMenu *IContextMenu_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *aPidls, UINT uItemCount)
146 {       IContextMenuImpl* cm;
147         UINT  u;
148
149         cm = (IContextMenuImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IContextMenuImpl));
150         ICOM_VTBL(cm)=&cmvt;
151         cm->ref = 1;
152         cm->pidl = ILClone(pidl);
153
154         cm->pSFParent = pSFParent;
155
156         if(pSFParent)
157            IShellFolder_AddRef(pSFParent);
158
159         cm->aPidls = NULL;
160
161         IContextMenu_AllocPidlTable(cm, uItemCount);
162
163         if(cm->aPidls)
164         { IContextMenu_FillPidlTable(cm, aPidls, uItemCount);
165         }
166
167         cm->bAllValues = 1;
168         for(u = 0; u < uItemCount; u++)
169         { cm->bAllValues &= (_ILIsValue(aPidls[u]) ? 1 : 0);
170         }
171         TRACE("(%p)->()\n",cm);
172         shell32_ObjCount++;
173         return (IContextMenu*)cm;
174 }
175
176 /**************************************************************************
177 *  IContextMenu_fnQueryInterface
178 */
179 static HRESULT WINAPI IContextMenu_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
180 {
181         ICOM_THIS(IContextMenuImpl, iface);
182
183         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
184
185         *ppvObj = NULL;
186
187         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
188         { *ppvObj = This; 
189         }
190         else if(IsEqualIID(riid, &IID_IContextMenu))  /*IContextMenu*/
191         { *ppvObj = This;
192         }   
193         else if(IsEqualIID(riid, &IID_IShellExtInit))  /*IShellExtInit*/
194         { FIXME("-- LPSHELLEXTINIT pointer requested\n");
195         }
196
197         if(*ppvObj)
198         { 
199           IContextMenu_AddRef((IContextMenu*)*ppvObj);      
200           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
201           return S_OK;
202         }
203         TRACE("-- Interface: E_NOINTERFACE\n");
204         return E_NOINTERFACE;
205 }
206
207 /**************************************************************************
208 *  IContextMenu_fnAddRef
209 */
210 static ULONG WINAPI IContextMenu_fnAddRef(IContextMenu *iface)
211 {
212         ICOM_THIS(IContextMenuImpl, iface);
213
214         TRACE("(%p)->(count=%lu)\n",This, This->ref);
215
216         shell32_ObjCount++;
217         return ++(This->ref);
218 }
219
220 /**************************************************************************
221 *  IContextMenu_fnRelease
222 */
223 static ULONG WINAPI IContextMenu_fnRelease(IContextMenu *iface)
224 {
225         ICOM_THIS(IContextMenuImpl, iface);
226
227         TRACE("(%p)->()\n",This);
228
229         shell32_ObjCount--;
230
231         if (!--(This->ref)) 
232         { TRACE(" destroying IContextMenu(%p)\n",This);
233
234           if(This->pSFParent)
235             IShellFolder_Release(This->pSFParent);
236
237           if(This->pidl)
238             SHFree(This->pidl);
239           
240           /*make sure the pidl is freed*/
241           if(This->aPidls)
242           { IContextMenu_FreePidlTable(This);
243           }
244
245           HeapFree(GetProcessHeap(),0,This);
246           return 0;
247         }
248         return This->ref;
249 }
250
251 /**************************************************************************
252 *  ICM_InsertItem()
253 */ 
254 void WINAPI _InsertMenuItem (
255         HMENU hmenu,
256         UINT indexMenu,
257         BOOL fByPosition,
258         UINT wID,
259         UINT fType,
260         LPSTR dwTypeData,
261         UINT fState)
262 {
263         MENUITEMINFOA   mii;
264
265         ZeroMemory(&mii, sizeof(mii));
266         mii.cbSize = sizeof(mii);
267         if (fType == MFT_SEPARATOR)
268         { mii.fMask = MIIM_ID | MIIM_TYPE;
269         }
270         else
271         { mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
272           mii.dwTypeData = dwTypeData;
273           mii.fState = fState;
274         }
275         mii.wID = wID;
276         mii.fType = fType;
277         InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
278 }
279 /**************************************************************************
280 * IContextMenu_fnQueryContextMenu()
281 */
282
283 static HRESULT WINAPI IContextMenu_fnQueryContextMenu(
284         IContextMenu *iface,
285         HMENU hmenu,
286         UINT indexMenu,
287         UINT idCmdFirst,
288         UINT idCmdLast,
289         UINT uFlags)
290 {
291         ICOM_THIS(IContextMenuImpl, iface);
292
293         BOOL    fExplore ;
294
295         TRACE("(%p)->(hmenu=%x indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
296
297         if(!(CMF_DEFAULTONLY & uFlags))
298         { if(!This->bAllValues) 
299           { /* folder menu */
300             fExplore = uFlags & CMF_EXPLORE;
301             if(fExplore) 
302             { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
303               _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
304             }
305             else
306             { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED|MFS_DEFAULT);
307               _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
308             }
309
310             if(uFlags & CMF_CANRENAME)
311             { _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
312               _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_RENAME, MFT_STRING, "&Rename", (IContextMenu_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED));
313             }
314           }
315           else  /* file menu */
316           { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED|MFS_DEFAULT);
317             if(uFlags & CMF_CANRENAME)
318             { _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
319               _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_RENAME, MFT_STRING, "&Rename", (IContextMenu_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED));
320             }
321           }
322           return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (IDM_LAST + 1));
323         }
324         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
325 }
326
327 /**************************************************************************
328 * IContextMenu_fnInvokeCommand()
329 */
330 static HRESULT WINAPI IContextMenu_fnInvokeCommand(
331         IContextMenu *iface,
332         LPCMINVOKECOMMANDINFO lpcmi)
333 {
334         ICOM_THIS(IContextMenuImpl, iface);
335
336         LPITEMIDLIST    pidlFQ;
337         SHELLEXECUTEINFOA       sei;
338         int   i;
339
340         TRACE("(%p)->(invcom=%p verb=%p wnd=%x)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);    
341
342         if(LOWORD(lpcmi->lpVerb) > IDM_LAST)
343           return E_INVALIDARG;
344
345         switch(LOWORD(lpcmi->lpVerb))
346         { case IDM_EXPLORE:
347           case IDM_OPEN:
348           /* Find the first item in the list that is not a value. These commands 
349             should never be invoked if there isn't at least one folder item in the list.*/
350
351             for(i = 0; This->aPidls[i]; i++)
352             { if(!_ILIsValue(This->aPidls[i]))
353                  break;
354             }
355
356             pidlFQ = ILCombine(This->pidl, This->aPidls[i]);
357
358             ZeroMemory(&sei, sizeof(sei));
359             sei.cbSize = sizeof(sei);
360             sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
361             sei.lpIDList = pidlFQ;
362             sei.lpClass = "folder";
363             sei.hwnd = lpcmi->hwnd;
364             sei.nShow = SW_SHOWNORMAL;
365
366             if(LOWORD(lpcmi->lpVerb) == IDM_EXPLORE)
367             { sei.lpVerb = "explore";
368             }
369             else
370             { sei.lpVerb = "open";
371             }
372             ShellExecuteExA(&sei);
373             SHFree(pidlFQ);
374             break;
375                 
376           case IDM_RENAME:
377             MessageBeep(MB_OK);
378             /*handle rename for the view here*/
379             break;          
380         }
381         return NOERROR;
382 }
383
384 /**************************************************************************
385 *  IContextMenu_fnGetCommandString()
386 */
387 static HRESULT WINAPI IContextMenu_fnGetCommandString(
388         IContextMenu *iface,
389         UINT idCommand,
390         UINT uFlags,
391         LPUINT lpReserved,
392         LPSTR lpszName,
393         UINT uMaxNameLen)
394 {       
395         ICOM_THIS(IContextMenuImpl, iface);
396
397         HRESULT  hr = E_INVALIDARG;
398
399         TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
400
401         switch(uFlags)
402         { case GCS_HELPTEXT:
403             hr = E_NOTIMPL;
404             break;
405
406           case GCS_VERBA:
407             switch(idCommand)
408             { case IDM_RENAME:
409                 strcpy((LPSTR)lpszName, "rename");
410                 hr = NOERROR;
411                 break;
412             }
413             break;
414
415              /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This 
416              case, you need to do the lstrcpyW to the pointer passed.*/
417           case GCS_VERBW:
418             switch(idCommand)
419             { case IDM_RENAME:
420                 lstrcpyAtoW((LPWSTR)lpszName, "rename");
421                 hr = NOERROR;
422                 break;
423             }
424             break;
425
426           case GCS_VALIDATE:
427             hr = NOERROR;
428             break;
429         }
430         TRACE("-- (%p)->(name=%s)\n",This, lpszName);
431         return hr;
432 }
433
434 /**************************************************************************
435 * IContextMenu_fnHandleMenuMsg()
436 * NOTES
437 *  should be only in IContextMenu2 and IContextMenu3
438 *  is nevertheless called from word95
439 */
440 static HRESULT WINAPI IContextMenu_fnHandleMenuMsg(
441         IContextMenu *iface,
442         UINT uMsg,
443         WPARAM wParam,
444         LPARAM lParam)
445 {
446         ICOM_THIS(IContextMenuImpl, iface);
447
448         TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
449
450         return E_NOTIMPL;
451 }
452