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