crypt32/tests: Use CryptAcquireContextA instead of CryptAcquireContextW.
[wine] / dlls / shell32 / shv_item_cmenu.c
index 7883662..6beb08f 100644 (file)
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
 #include <string.h>
 
+#define COBJMACROS
 #define NONAMELESSUNION
 #define NONAMELESSSTRUCT
+
 #include "winerror.h"
 #include "wine/debug.h"
 
 #include "windef.h"
 #include "wingdi.h"
 #include "pidl.h"
-#include "shlguid.h"
 #include "undocshell.h"
 #include "shlobj.h"
 
 #include "shell32_main.h"
 #include "shellfolder.h"
 
+#include "shresdef.h"
+
 WINE_DEFAULT_DEBUG_CHANNEL(shell);
 
 /**************************************************************************
 *  IContextMenu Implementation
 */
 typedef struct
-{      ICOM_VFIELD(IContextMenu2);
-       DWORD           ref;
+{      const IContextMenu2Vtbl *lpVtbl;
+       LONG            ref;
        IShellFolder*   pSFParent;
        LPITEMIDLIST    pidl;           /* root pidl */
        LPITEMIDLIST    *apidl;         /* array of child pidls */
@@ -51,7 +54,7 @@ typedef struct
 } ItemCmImpl;
 
 
-static struct ICOM_VTABLE(IContextMenu2) cmvt;
+static const IContextMenu2Vtbl cmvt;
 
 /**************************************************************************
 * ISvItemCm_CanRenameItems()
@@ -76,11 +79,11 @@ static BOOL ISvItemCm_CanRenameItems(ItemCmImpl *This)
 /**************************************************************************
 *   ISvItemCm_Constructor()
 */
-IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *apidl, UINT cidl)
+IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, const LPCITEMIDLIST *apidl, UINT cidl)
 {      ItemCmImpl* cm;
        UINT  u;
 
-       cm = (ItemCmImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
+       cm = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ItemCmImpl));
        cm->lpVtbl = &cmvt;
        cm->ref = 1;
        cm->pidl = ILClone(pidl);
@@ -107,7 +110,7 @@ IContextMenu2 *ISvItemCm_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl
 */
 static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID riid, LPVOID *ppvObj)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
 
        TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
 
@@ -139,11 +142,12 @@ static HRESULT WINAPI ISvItemCm_fnQueryInterface(IContextMenu2 *iface, REFIID ri
 */
 static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
+       ULONG refCount = InterlockedIncrement(&This->ref);
 
-       TRACE("(%p)->(count=%lu)\n",This, This->ref);
+       TRACE("(%p)->(count=%u)\n", This, refCount - 1);
 
-       return ++(This->ref);
+       return refCount;
 }
 
 /**************************************************************************
@@ -151,44 +155,39 @@ static ULONG WINAPI ISvItemCm_fnAddRef(IContextMenu2 *iface)
 */
 static ULONG WINAPI ISvItemCm_fnRelease(IContextMenu2 *iface)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
+       ULONG refCount = InterlockedDecrement(&This->ref);
 
-       TRACE("(%p)->()\n",This);
+       TRACE("(%p)->(count=%i)\n", This, refCount + 1);
 
-       if (!--(This->ref))
+       if (!refCount)
        {
          TRACE(" destroying IContextMenu(%p)\n",This);
 
          if(This->pSFParent)
            IShellFolder_Release(This->pSFParent);
 
-         if(This->pidl)
-           SHFree(This->pidl);
+         SHFree(This->pidl);
 
          /*make sure the pidl is freed*/
          _ILFreeaPidl(This->apidl, This->cidl);
 
          HeapFree(GetProcessHeap(),0,This);
-         return 0;
        }
-       return This->ref;
+       return refCount;
 }
 
-/**************************************************************************
-*  ICM_InsertItem()
-*/
-void WINAPI _InsertMenuItem (
+static void _InsertMenuItemW (
        HMENU hmenu,
        UINT indexMenu,
        BOOL fByPosition,
        UINT wID,
        UINT fType,
-       LPSTR dwTypeData,
+       LPWSTR dwTypeData,
        UINT fState)
 {
-       MENUITEMINFOA   mii;
+       MENUITEMINFOW   mii;
 
-       ZeroMemory(&mii, sizeof(mii));
        mii.cbSize = sizeof(mii);
        if (fType == MFT_SEPARATOR)
        {
@@ -202,12 +201,12 @@ void WINAPI _InsertMenuItem (
        }
        mii.wID = wID;
        mii.fType = fType;
-       InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
+       InsertMenuItemW( hmenu, indexMenu, fByPosition, &mii);
 }
+
 /**************************************************************************
 * ISvItemCm_fnQueryContextMenu()
 */
-
 static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
        IContextMenu2 *iface,
        HMENU hmenu,
@@ -216,38 +215,43 @@ static HRESULT WINAPI ISvItemCm_fnQueryContextMenu(
        UINT idCmdLast,
        UINT uFlags)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
 
        TRACE("(%p)->(hmenu=%p indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
 
-       if(!(CMF_DEFAULTONLY & uFlags))
+       if (idCmdFirst != 0)
+         FIXME("We should use idCmdFirst=%d and idCmdLast=%d for command ids\n", idCmdFirst, idCmdLast);
+
+       if(!(CMF_DEFAULTONLY & uFlags) && This->cidl>0)
        {
+          HMENU hmenures = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_SHV_FILE));
+
          if(uFlags & CMF_EXPLORE)
+            RemoveMenu(hmenures, FCIDM_SHVIEW_OPEN, MF_BYCOMMAND);
+
+          Shell_MergeMenus(hmenu, GetSubMenu(hmenures, 0), indexMenu, idCmdFirst, idCmdLast, MM_SUBMENUSHAVEIDS);
+
+          DestroyMenu(hmenures);
+
+         if(This->bAllValues)
          {
-           if(This->bAllValues)
-           {
-             _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
-             _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
-           }
-           else
-           {
-             _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
-             _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
-           }
-         }
-         else
-         {
-           _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_OPEN, MFT_STRING, "&Select", MFS_ENABLED|MFS_DEFAULT);
+            MENUITEMINFOW mi;
+            WCHAR str[255];
+            mi.cbSize = sizeof(mi);
+            mi.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE;
+            mi.dwTypeData = str;
+            mi.cch = 255;
+            GetMenuItemInfoW(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND, &mi);
+            RemoveMenu(hmenu, FCIDM_SHVIEW_EXPLORE, MF_BYCOMMAND);
+            _InsertMenuItemW(hmenu, (uFlags & CMF_EXPLORE) ? 1 : 2, MF_BYPOSITION, FCIDM_SHVIEW_EXPLORE, MFT_STRING, str, MFS_ENABLED);
          }
-         _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
-         _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_COPY, MFT_STRING, "&Copy", MFS_ENABLED);
-         _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_CUT, MFT_STRING, "&Cut", MFS_ENABLED);
 
-         _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
-         _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_DELETE, MFT_STRING, "&Delete", MFS_ENABLED);
+         SetMenuDefaultItem(hmenu, 0, MF_BYPOSITION);
 
-         if(uFlags & CMF_CANRENAME)
-           _InsertMenuItem(hmenu, indexMenu++, TRUE, FCIDM_SHVIEW_RENAME, MFT_STRING, "&Rename", ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
+         if(uFlags & ~CMF_CANRENAME)
+            RemoveMenu(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND);
+          else
+            EnableMenuItem(hmenu, FCIDM_SHVIEW_RENAME, MF_BYCOMMAND | ISvItemCm_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED);
 
          return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (FCIDM_SHVIEWLAST));
        }
@@ -265,7 +269,7 @@ static void DoOpenExplore(
        HWND hwnd,
        LPCSTR verb)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
 
        UINT i, bFolderFound = FALSE;
        LPITEMIDLIST    pidlFQ;
@@ -291,7 +295,7 @@ static void DoOpenExplore(
        sei.cbSize = sizeof(sei);
        sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
        sei.lpIDList = pidlFQ;
-       sei.lpClass = "folder";
+       sei.lpClass = "Folder";
        sei.hwnd = hwnd;
        sei.nShow = SW_SHOWNORMAL;
        sei.lpVerb = verb;
@@ -306,7 +310,7 @@ static void DoRename(
        IContextMenu2 *iface,
        HWND hwnd)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
 
        LPSHELLBROWSER  lpSB;
        LPSHELLVIEW     lpSV;
@@ -333,7 +337,7 @@ static void DoRename(
  */
 static void DoDelete(IContextMenu2 *iface)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
        ISFHelper * psfhlp;
 
        IShellFolder_QueryInterface(This->pSFParent, &IID_ISFHelper, (LPVOID*)&psfhlp);
@@ -354,7 +358,7 @@ static BOOL DoCopyOrCut(
        HWND hwnd,
        BOOL bCut)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
 
        LPSHELLBROWSER  lpSB;
        LPSHELLVIEW     lpSV;
@@ -362,62 +366,20 @@ static BOOL DoCopyOrCut(
 
        TRACE("(%p)->(wnd=%p,bCut=0x%08x)\n",This, hwnd, bCut);
 
-       if(GetShellOle())
+       /* get the active IShellView */
+       if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
        {
-         /* get the active IShellView */
-         if ((lpSB = (LPSHELLBROWSER)SendMessageA(hwnd, CWM_GETISHELLBROWSER,0,0)))
+         if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
          {
-           if (SUCCEEDED(IShellBrowser_QueryActiveShellView(lpSB, &lpSV)))
+           if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
            {
-             if (SUCCEEDED(IShellView_GetItemObject(lpSV, SVGIO_SELECTION, &IID_IDataObject, (LPVOID*)&lpDo)))
-             {
-               pOleSetClipboard(lpDo);
-               IDataObject_Release(lpDo);
-             }
-             IShellView_Release(lpSV);
+             OleSetClipboard(lpDo);
+             IDataObject_Release(lpDo);
            }
+           IShellView_Release(lpSV);
          }
        }
        return TRUE;
-#if 0
-/*
-  the following code does the copy operation witout ole32.dll
-  we might need this possibility too (js)
-*/
-       BOOL bSuccess = FALSE;
-
-       TRACE("(%p)\n", iface);
-
-       if(OpenClipboard(NULL))
-       {
-         if(EmptyClipboard())
-         {
-           IPersistFolder2 * ppf2;
-           IShellFolder_QueryInterface(This->pSFParent, &IID_IPersistFolder2, (LPVOID*)&ppf2);
-           if (ppf2)
-           {
-             LPITEMIDLIST pidl;
-             IPersistFolder2_GetCurFolder(ppf2, &pidl);
-             if(pidl)
-             {
-               HGLOBAL hMem;
-
-               hMem = RenderHDROP(pidl, This->apidl, This->cidl);
-
-               if(SetClipboardData(CF_HDROP, hMem))
-               {
-                 bSuccess = TRUE;
-               }
-               SHFree(pidl);
-             }
-             IPersistFolder2_Release(ppf2);
-           }
-
-         }
-         CloseClipboard();
-       }
-       return bSuccess;
-#endif
 }
 /**************************************************************************
 * ISvItemCm_fnInvokeCommand()
@@ -426,7 +388,7 @@ static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
        IContextMenu2 *iface,
        LPCMINVOKECOMMANDINFO lpcmi)
 {
-    ICOM_THIS(ItemCmImpl, iface);
+    ItemCmImpl *This = (ItemCmImpl *)iface;
 
     if (lpcmi->cbSize != sizeof(CMINVOKECOMMANDINFO))
         FIXME("Is an EX structure\n");
@@ -487,17 +449,17 @@ static HRESULT WINAPI ISvItemCm_fnInvokeCommand(
 */
 static HRESULT WINAPI ISvItemCm_fnGetCommandString(
        IContextMenu2 *iface,
-       UINT idCommand,
+       UINT_PTR idCommand,
        UINT uFlags,
        UINT* lpReserved,
        LPSTR lpszName,
        UINT uMaxNameLen)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
 
        HRESULT  hr = E_INVALIDARG;
 
-       TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
+       TRACE("(%p)->(idcom=%lx flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
 
        switch(uFlags)
        {
@@ -510,7 +472,7 @@ static HRESULT WINAPI ISvItemCm_fnGetCommandString(
            switch(idCommand)
            {
              case FCIDM_SHVIEW_RENAME:
-               strcpy((LPSTR)lpszName, "rename");
+               strcpy(lpszName, "rename");
                hr = NOERROR;
                break;
            }
@@ -548,16 +510,15 @@ static HRESULT WINAPI ISvItemCm_fnHandleMenuMsg(
        WPARAM wParam,
        LPARAM lParam)
 {
-       ICOM_THIS(ItemCmImpl, iface);
+       ItemCmImpl *This = (ItemCmImpl *)iface;
 
-       TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
+       TRACE("(%p)->(msg=%x wp=%lx lp=%lx)\n",This, uMsg, wParam, lParam);
 
        return E_NOTIMPL;
 }
 
-static struct ICOM_VTABLE(IContextMenu2) cmvt =
+static const IContextMenu2Vtbl cmvt =
 {
-       ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
        ISvItemCm_fnQueryInterface,
        ISvItemCm_fnAddRef,
        ISvItemCm_fnRelease,