[SHELL32] Expose IID_CDefView in our CDefView implementation, this fixes a crash...
[reactos.git] / reactos / dll / win32 / shell32 / CDefView.cpp
index 580ce5b..09cd0aa 100644 (file)
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
- * FIXME: The order by part of the background context menu should be
- * built according to the columns shown.
- *
  * FIXME: CheckToolbar: handle the "new folder" and "folder up" button
  */
 
 /*
 TODO:
 - Load/Save the view state from/into the stream provided by the ShellBrowser.
-- Code to merge menus in the shellbrowser is incorrect.
-- Move the background context menu creation into shell view. It should store the
-   shell view HWND to send commands.
-- Shell view should do SetCommandTarget on internet toolbar.
 - When editing starts on item, set edit text to for editing value.
-- When shell view is called back for item info, let listview save the value.
 - Fix shell view to handle view mode popup exec.
 - The background context menu should have a pidl just like foreground menus. This
    causes crashes when dynamic handlers try to use the NULL pidl.
-- The SHELLDLL_DefView should not be filled with blue unconditionally. This causes
-   annoying flashing of blue even on XP, and is not correct.
 - Reorder of columns doesn't work - might be bug in comctl32
 */
 
@@ -84,8 +74,11 @@ class CDefView :
         CListView                 m_ListView;
         HWND                      m_hWndParent;
         FOLDERSETTINGS            m_FolderSettings;
-        HMENU                     m_hMenu;
-        BOOL                      m_menusLoaded;
+        HMENU                     m_hMenu;                /* Handle to the menu bar of the browser */
+        HMENU                     m_hMenuArrangeModes;    /* Handle to the popup menu with the arrange modes */
+        HMENU                     m_hMenuViewModes;       /* Handle to the popup menu with the view modes */
+        HMENU                     m_hContextMenu;         /* Handle to the open context menu */
+        BOOL                      m_bmenuBarInitialized;
         UINT                      m_uState;
         UINT                      m_cidl;
         PCUITEMID_CHILD          *m_apidl;
@@ -103,13 +96,13 @@ class CDefView :
         LONG                      m_iDragOverItem;      /* Dragged over item's index, iff m_pCurDropTarget != NULL */
         UINT                      m_cScrollDelay;       /* Send a WM_*SCROLL msg every 250 ms during drag-scroll */
         POINT                     m_ptLastMousePos;     /* Mouse position at last DragOver call */
+        POINT                     m_ptFirstMousePos;    /* Mouse position when the drag operation started */
         //
         CComPtr<IContextMenu>     m_pCM;
 
         BOOL                      m_isEditing;
 
         CLSID m_Category;
-        HMENU m_hView;
         BOOL m_Destroyed;
     private:
 
@@ -139,9 +132,11 @@ class CDefView :
         BOOLEAN LV_ProdItem(PCUITEMID_CHILD pidl);
         static INT CALLBACK fill_list(LPVOID ptr, LPVOID arg);
         HRESULT FillList();
-        HMENU BuildFileMenu();
-        void PrepareShowFileMenu(HMENU hSubMenu);
-        void PrepareShowViewMenu(HMENU hSubMenu);
+        HRESULT FillFileMenu();
+        HRESULT FillEditMenu();
+        HRESULT FillViewMenu();
+        HRESULT FillArrangeAsMenu(HMENU hmenuArrange);
+        HRESULT CheckViewMode(HMENU hmenuView);
         UINT GetSelections();
         HRESULT OpenSelectedItems();
         void OnDeactivate();
@@ -330,6 +325,7 @@ class CDefView :
         // Windows returns E_NOINTERFACE for IOleWindow
         // COM_INTERFACE_ENTRY_IID(IID_IOleWindow, IOleWindow)
         COM_INTERFACE_ENTRY_IID(IID_IShellView, IShellView)
+        COM_INTERFACE_ENTRY_IID(IID_CDefView, IShellView)
         COM_INTERFACE_ENTRY_IID(IID_IShellView2, IShellView2)
         COM_INTERFACE_ENTRY_IID(IID_IFolderView, IFolderView)
         COM_INTERFACE_ENTRY_IID(IID_IShellFolderView, IShellFolderView)
@@ -359,7 +355,10 @@ CDefView::CDefView() :
     m_ListView(),
     m_hWndParent(NULL),
     m_hMenu(NULL),
-    m_menusLoaded(FALSE),
+    m_hMenuArrangeModes(NULL),
+    m_hMenuViewModes(NULL),
+    m_hContextMenu(NULL),
+    m_bmenuBarInitialized(FALSE),
     m_uState(0),
     m_cidl(0),
     m_apidl(NULL),
@@ -371,7 +370,6 @@ CDefView::CDefView() :
     m_iDragOverItem(0),
     m_cScrollDelay(0),
     m_isEditing(FALSE),
-    m_hView(NULL),
     m_Destroyed(FALSE)
 {
     ZeroMemory(&m_FolderSettings, sizeof(m_FolderSettings));
@@ -593,8 +591,8 @@ void CDefView::UpdateListColors()
          */
         m_ListView.SetBkColor(CLR_NONE);
 
-        RegGetValueW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced",
-                     L"ListviewShadow", RRF_RT_DWORD, NULL, &bDropShadow, &cbDropShadow);
+        SHGetValueW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced",
+                     L"ListviewShadow", NULL, &bDropShadow, &cbDropShadow);
         if (bDropShadow)
         {
             /* Set the icon background transparent */
@@ -631,6 +629,8 @@ BOOL CDefView::InitList()
 
     m_ListView.DeleteAllItems();
 
+    m_hMenuArrangeModes = CreateMenu();
+
     if (m_pSF2Parent)
     {
         for (int i = 0; 1; i++)
@@ -640,7 +640,10 @@ BOOL CDefView::InitList()
             StrRetToStrNW( szTemp, 50, &sd.str, NULL);
             m_ListView.InsertColumn(i, szTemp, sd.fmt, sd.cxChar * 8);
 
+            InsertMenuW(m_hMenuArrangeModes, -1, MF_STRING, 0x30 + i,  szTemp);
         }
+
+        InsertMenuW(m_hMenuArrangeModes, -1, MF_BYPOSITION | MF_SEPARATOR, 0, 0);
     }
     else
     {
@@ -1028,117 +1031,126 @@ LRESULT CDefView::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandl
  *    #### Handling of the menus ####
  */
 
-HMENU CDefView::BuildFileMenu()
-{
-    HRESULT hr;
-    CComPtr<IContextMenu> cm;
-
-    GetSelections();
+extern "C" DWORD WINAPI SHMenuIndexFromID(HMENU hMenu, UINT uID);
 
-    hr = m_pSFParent->GetUIObjectOf(m_hWnd, m_cidl, m_apidl, IID_NULL_PPV_ARG(IContextMenu, &cm));
-    if (FAILED(hr))
-        return NULL;
-
-    HMENU hmenu = CreatePopupMenu();
+HMENU GetSubmenuByID(HMENU hmenu, UINT id)
+{
+    MENUITEMINFOW mii = {sizeof(mii), MIIM_SUBMENU};
+    if (::GetMenuItemInfoW(hmenu, id, FALSE, &mii))
+        return mii.hSubMenu;
 
-    // FIXME: get proper numbers ?
-    const UINT first = 0x7800;
-    const UINT last  = 0x7A00;
-    hr = cm->QueryContextMenu(hmenu, 0, first, last, 0);
-    if (FAILED(hr))
-        return NULL;
+    return NULL;
+}
 
-    // TODO: filter or something
+/* ReallyGetMenuItemID returns the id of an item even if it opens a submenu,
+   GetMenuItemID returns -1 if the specified item opens a submenu */
+UINT ReallyGetMenuItemID(HMENU hmenu, int i)
+{
+    MENUITEMINFOW mii = {sizeof(mii), MIIM_ID};
+    if (::GetMenuItemInfoW(hmenu, i, TRUE, &mii))
+        return mii.wID;
 
-    return hmenu;
+    return UINT_MAX;
 }
 
-void CDefView::PrepareShowFileMenu(HMENU hSubMenu)
+HRESULT CDefView::FillFileMenu()
 {
-    TRACE("(%p)->(submenu=%p) stub\n", this, hSubMenu);
-
-    if (!hSubMenu)
-        return;
+    HMENU hFileMenu = GetSubmenuByID(m_hMenu, FCIDM_MENU_FILE);
+    if (!hFileMenu)
+        return E_FAIL;
 
     /* Cleanup the items added previously */
-    for (int i = 0; i < GetMenuItemCount(hSubMenu); )
+    for (int i = GetMenuItemCount(hFileMenu) - 1; i >= 0; i--)
     {
-        MENUITEMINFOW mii;
-        mii.cbSize = sizeof(mii);
-        mii.fMask = MIIM_ID;
-        GetMenuItemInfoW(hSubMenu, i, TRUE, &mii);
-
-        if (mii.wID < 0x8000)
-        {
-            DeleteMenu(hSubMenu, i, MF_BYPOSITION);
-        }
-        else
-        {
-            i++;
-        }
+        UINT id = GetMenuItemID(hFileMenu, i);
+        if (id < FCIDM_BROWSERFIRST || id > FCIDM_BROWSERLAST)
+            DeleteMenu(hFileMenu, i, MF_BYPOSITION);
     }
 
-#if 0
-    /* FIXME/TODO: Reenable when they implemented AND localizable (not hardcoded). */
-    /* Insert This item at the beginning of the menu. */
-    _InsertMenuItemW(hSubMenu, 0, TRUE, 0, MFT_SEPARATOR, NULL, MFS_ENABLED);
-    _InsertMenuItemW(hSubMenu, 0, TRUE, IDM_MYFILEITEM + 4, MFT_STRING, L"Properties", MFS_DISABLED);
-    _InsertMenuItemW(hSubMenu, 0, TRUE, IDM_MYFILEITEM + 3, MFT_STRING, L"Rename", MFS_DISABLED);
-    _InsertMenuItemW(hSubMenu, 0, TRUE, IDM_MYFILEITEM + 2, MFT_STRING, L"Delete", MFS_DISABLED);
-    _InsertMenuItemW(hSubMenu, 0, TRUE, IDM_MYFILEITEM + 1, MFT_STRING, L"Create Shortcut", MFS_DISABLED);
-    _InsertMenuItemW(hSubMenu, 0, TRUE, 0, MFT_SEPARATOR, NULL, MFS_ENABLED);
-    _InsertMenuItemW(hSubMenu, 0, TRUE, IDM_MYFILEITEM, MFT_STRING, L"New", MFS_ENABLED);
-#endif
+    /* Store the context menu in m_pCM and keep it in order to invoke the selected command later on */
+    HRESULT hr = GetItemObject(SVGIO_SELECTION, IID_PPV_ARG(IContextMenu, &m_pCM));
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
 
-    HMENU menubase = BuildFileMenu();
-    if (menubase)
-    {
-        int count = ::GetMenuItemCount(menubase);
-        int count2 = ::GetMenuItemCount(hSubMenu);
+    IUnknown_SetSite(m_pCM, (IShellView *)this);
 
-        if (count2 > 0 && count > 0)
-        {
-            _InsertMenuItemW(hSubMenu, 0, TRUE, 0, MFT_SEPARATOR, NULL, MFS_ENABLED);
-        }
+    HMENU hmenu = CreatePopupMenu();
 
-        for (int i = count-1; i >= 0; i--)
-        {
-            WCHAR label[128];
+    hr = m_pCM->QueryContextMenu(hmenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, 0);
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
 
-            MENUITEMINFOW mii = { 0 };
-            mii.cbSize = sizeof(mii);
-            mii.fMask = MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS | MIIM_DATA | MIIM_STRING | MIIM_BITMAP | MIIM_FTYPE;
-            mii.dwTypeData = label;
-            mii.cch = _countof(label);
-            ::GetMenuItemInfoW(menubase, i, TRUE, &mii);
+    // TODO: filter or something
 
-            TRACE("Adding item %d label %S type %d\n", mii.wID, mii.dwTypeData, mii.fType);
+    Shell_MergeMenus(hFileMenu, hmenu, 0, 0, 0xFFFF, MM_ADDSEPARATOR | MM_SUBMENUSHAVEIDS);
 
-            mii.fType |= MFT_RADIOCHECK;
+    ::DestroyMenu(hmenu);
 
-            ::InsertMenuItemW(hSubMenu, 0, TRUE, &mii);
-        }
+    return S_OK;
+}
+
+HRESULT CDefView::FillEditMenu()
+{
+    HMENU hEditMenu = GetSubmenuByID(m_hMenu, FCIDM_MENU_EDIT);
+    if (!hEditMenu)
+        return E_FAIL;
 
+    HMENU hmenuContents = ::LoadMenuW(shell32_hInstance, L"MENU_003");
+    if (!hmenuContents)
+        return E_FAIL;
 
-        ::DestroyMenu(menubase);
-    }
-    TRACE("--\n");
+    Shell_MergeMenus(hEditMenu, hmenuContents, 0, 0, 0xFFFF, 0);
+
+    ::DestroyMenu(hmenuContents);
+
+    return S_OK;
 }
 
-void CDefView::PrepareShowViewMenu(HMENU hSubMenu)
+HRESULT CDefView::FillViewMenu()
 {
-    TRACE("(%p)->(submenu=%p)\n", this, hSubMenu);
+    HMENU hViewMenu = GetSubmenuByID(m_hMenu, FCIDM_MENU_VIEW);
+    if (!hViewMenu)
+        return E_FAIL;
 
-    if (!hSubMenu)
-        return;
+    m_hMenuViewModes = ::LoadMenuW(shell32_hInstance, L"MENU_001");
+    if (!m_hMenuViewModes)
+        return E_FAIL;
 
+    UINT i = SHMenuIndexFromID(hViewMenu, FCIDM_MENU_VIEW_SEP_OPTIONS);
+    Shell_MergeMenus(hViewMenu, m_hMenuViewModes, i, 0, 0xFFFF, MM_ADDSEPARATOR | MM_DONTREMOVESEPS | MM_SUBMENUSHAVEIDS);
+
+    return S_OK;
+}
+
+HRESULT CDefView::FillArrangeAsMenu(HMENU hmenuArrange)
+{
+    /* We only need to fill this once */
+    if (GetMenuItemID(hmenuArrange, 0) == FCIDM_SHVIEW_AUTOARRANGE)
+    {
+        Shell_MergeMenus(hmenuArrange, m_hMenuArrangeModes, 0, 0, 0xFFFF,0);
+    }
+
+    /* Also check the menu item according to which we sort */
+    CheckMenuRadioItem(hmenuArrange, 
+                       0x30, 
+                       0x100, 
+                       m_sortInfo.nHeaderID + 0x30, 
+                       MF_BYCOMMAND);
+
+    return S_OK;
+}
+
+HRESULT CDefView::CheckViewMode(HMENU hmenuView)
+{
     if (m_FolderSettings.ViewMode >= FVM_FIRST && m_FolderSettings.ViewMode <= FVM_LAST)
     {
         UINT iItemFirst = FCIDM_SHVIEW_BIGICON;
         UINT iItemLast = iItemFirst + FVM_LAST - FVM_FIRST;
         UINT iItem = iItemFirst + m_FolderSettings.ViewMode - FVM_FIRST;
-        CheckMenuRadioItem(hSubMenu, iItemFirst, iItemLast, iItem, MF_BYCOMMAND);
+        CheckMenuRadioItem(hmenuView, iItemFirst, iItemLast, iItem, MF_BYCOMMAND);
     }
+
+    return S_OK;
 }
 
 /**********************************************************
@@ -1192,7 +1204,11 @@ HRESULT CDefView::InvokeContextMenuCommand(UINT uCommand)
     if (GetKeyState(VK_CONTROL) & 0x8000)
         cmi.fMask |= CMIC_MASK_CONTROL_DOWN;
 
-    return m_pCM->InvokeCommand(&cmi);
+    HRESULT hr = m_pCM->InvokeCommand(&cmi);
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
+
+    return S_OK;
 }
 
 /**********************************************************
@@ -1217,13 +1233,13 @@ HRESULT CDefView::OpenSelectedItems()
         return E_FAIL;
 
     hResult = GetItemObject(SVGIO_SELECTION, IID_PPV_ARG(IContextMenu, &m_pCM));
-    if (FAILED(hResult))
+    if (FAILED_UNEXPECTEDLY(hResult))
         goto cleanup;
 
     IUnknown_SetSite(m_pCM, (IShellView *)this);
 
     hResult = m_pCM->QueryContextMenu(hMenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY);
-    if (FAILED(hResult))
+    if (FAILED_UNEXPECTEDLY(hResult))
         goto cleanup;
 
     uCommand = GetMenuDefaultItem(hMenu, FALSE, 0);
@@ -1256,35 +1272,30 @@ LRESULT CDefView::OnContextMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &b
 {
     WORD    x, y;
     UINT    uCommand;
-    HMENU   hMenu;
     HRESULT hResult;
 
-    // for some reason I haven't figured out, we sometimes recurse into this method
-    if (m_pCM != NULL)
-        return 0;
-
     x = LOWORD(lParam);
     y = HIWORD(lParam);
 
     TRACE("(%p)->(0x%08x 0x%08x) stub\n", this, x, y);
 
-    hMenu = CreatePopupMenu();
-    if (!hMenu) 
+    m_hContextMenu = CreatePopupMenu();
+    if (!m_hContextMenu) 
         return E_FAIL;
 
     m_cidl = m_ListView.GetSelectedCount();
 
     hResult = GetItemObject( m_cidl ? SVGIO_SELECTION : SVGIO_BACKGROUND, IID_PPV_ARG(IContextMenu, &m_pCM));
-    if (FAILEDhResult))
+    if (FAILED_UNEXPECTEDLY(hResult))
         goto cleanup;
 
     IUnknown_SetSite(m_pCM, (IShellView *)this);
 
-    hResult = m_pCM->QueryContextMenu(hMenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
-    if (FAILEDhResult))
+    hResult = m_pCM->QueryContextMenu(m_hContextMenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
+    if (FAILED_UNEXPECTEDLY(hResult))
         goto cleanup;
 
-    uCommand = TrackPopupMenu(hMenu,
+    uCommand = TrackPopupMenu(m_hContextMenu,
                               TPM_LEFTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
                               x, y, 0, m_hWnd, NULL);
     if (uCommand == 0)
@@ -1302,8 +1313,11 @@ cleanup:
         m_pCM.Release();
     }
 
-    if (hMenu)
-        DestroyMenu(hMenu);
+    if (m_hContextMenu)
+    {
+        DestroyMenu(m_hContextMenu);
+        m_hContextMenu = NULL;
+    }
 
     return 0;
 }
@@ -1318,13 +1332,13 @@ LRESULT CDefView::OnExplorerCommand(UINT uCommand, BOOL bUseSelection)
         return 0;
 
     hResult = GetItemObject( bUseSelection ? SVGIO_SELECTION : SVGIO_BACKGROUND, IID_PPV_ARG(IContextMenu, &m_pCM));
-    if (FAILED( hResult))
+    if (FAILED_UNEXPECTEDLY( hResult))
         goto cleanup;
 
     IUnknown_SetSite(m_pCM, (IShellView *)this);
 
     hResult = m_pCM->QueryContextMenu(hMenu, 0, FCIDM_SHVIEWFIRST, FCIDM_SHVIEWLAST, CMF_NORMAL);
-    if (FAILED( hResult))
+    if (FAILED_UNEXPECTEDLY( hResult))
         goto cleanup;
 
     InvokeContextMenuCommand(uCommand);
@@ -1401,87 +1415,12 @@ void CDefView::DoActivate(UINT uState)
     }
     else
     {
-        if (m_hMenu)
+        if(m_hMenu && !m_bmenuBarInitialized)
         {
-            if (!m_menusLoaded)
-            {
-                MENUITEMINFOW mii = { 0 };
-
-                /* initialize EDIT menu */
-                mii.cbSize = sizeof(mii);
-                mii.fMask = MIIM_SUBMENU;
-                if (::GetMenuItemInfoW(m_hMenu, FCIDM_MENU_EDIT, FALSE, &mii))
-                {
-                    HMENU hSubMenu = mii.hSubMenu;
-
-                    HMENU menubase = ::LoadMenuW(shell32_hInstance, L"MENU_003");
-
-                    int count = ::GetMenuItemCount(menubase);
-                    for (int i = 0; i < count; i++)
-                    {
-                        WCHAR label[128];
-
-                        ZeroMemory(&mii, sizeof(mii));
-                        mii.cbSize = sizeof(mii);
-                        mii.fMask = MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS | MIIM_DATA | MIIM_STRING | MIIM_BITMAP | MIIM_FTYPE;
-                        mii.dwTypeData = label;
-                        mii.cch = _countof(label);
-                        ::GetMenuItemInfoW(menubase, i, TRUE, &mii);
-
-                        TRACE("Adding item %d label %S type %d\n", mii.wID, mii.dwTypeData, mii.fType);
-
-                        mii.fType |= MFT_RADIOCHECK;
-
-                        ::InsertMenuItemW(hSubMenu, FCIDM_MENU_VIEW_SEP_OPTIONS, FALSE, &mii);
-                    }
-
-                    ::DestroyMenu(menubase);
-                }
-
-                /* initialize VIEW menu */
-
-                mii.cbSize = sizeof(mii);
-                mii.fMask = MIIM_SUBMENU;
-                if (::GetMenuItemInfoW(m_hMenu, FCIDM_MENU_VIEW, FALSE, &mii))
-                {
-                    HMENU menubase = ::LoadMenuW(shell32_hInstance, L"MENU_001");
-
-                    HMENU hSubMenu = mii.hSubMenu;
-
-                    m_hView = CreatePopupMenu();
-
-                    _InsertMenuItemW(hSubMenu, FCIDM_MENU_VIEW_SEP_OPTIONS, FALSE, 0, MFT_SEPARATOR, NULL, MFS_ENABLED);
-
-                    int count = ::GetMenuItemCount(menubase);
-                    for (int i = 0; i < count; i++)
-                    {
-                        WCHAR label[128];
-
-                        ZeroMemory(&mii, sizeof(mii));
-                        mii.cbSize = sizeof(mii);
-                        mii.fMask = MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_CHECKMARKS | MIIM_DATA | MIIM_STRING | MIIM_BITMAP | MIIM_FTYPE;
-                        mii.dwTypeData = label;
-                        mii.cch = _countof(label);
-                        ::GetMenuItemInfoW(menubase, i, TRUE, &mii);
-
-                        ::AppendMenuW(m_hView, mii.fType, mii.wID, mii.dwTypeData);
-
-                        TRACE("Adding item %d label %S type %d\n", mii.wID, mii.dwTypeData, mii.fType);
-
-                        mii.fType |= MFT_RADIOCHECK;
-
-                        ::InsertMenuItemW(hSubMenu, FCIDM_MENU_VIEW_SEP_OPTIONS, FALSE, &mii);
-                    }
-
-                    ::DestroyMenu(menubase);
-                }
-
-
-                TRACE("-- before fnSetMenuSB\n");
-                m_pShellBrowser->SetMenuSB(m_hMenu, 0, m_hWnd);
-
-                m_menusLoaded = TRUE;
-            }
+            FillEditMenu();
+            FillViewMenu();
+            m_pShellBrowser->SetMenuSB(m_hMenu, 0, m_hWnd);
+            m_bmenuBarInitialized = TRUE;
         }
 
         if (SVUIA_ACTIVATE_FOCUS == uState)
@@ -1597,6 +1536,10 @@ LRESULT CDefView::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHand
             m_ListView.SortItems(ListViewCompareItems, this);
             break;
 
+        case FCIDM_SHVIEW_SNAPTOGRID:
+        case FCIDM_SHVIEW_AUTOARRANGE:
+            //FIXME
+            break;
         case FCIDM_SHVIEW_SELECTALL:
             m_ListView.SetItemState(-1, LVIS_SELECTED, LVIS_SELECTED);
             break;
@@ -1623,7 +1566,11 @@ LRESULT CDefView::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHand
         case FCIDM_SHVIEW_NEWFOLDER:
             return OnExplorerCommand(dwCmdID, FALSE);
         default:
-            TRACE("-- COMMAND 0x%04x unhandled\n", dwCmdID);
+            /* WM_COMMAND messages from the file menu are routed to the CDefView so as to let m_pCM handle the command */
+            if (m_pCM)
+            {
+                InvokeContextMenuCommand(dwCmdID);
+            }
     }
 
     return 0;
@@ -1737,11 +1684,8 @@ LRESULT CDefView::OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandl
                 if (m_pSF2Parent)
                 {
                     SHELLDETAILS sd;
-                    if (FAILED(m_pSF2Parent->GetDetailsOf(pidl, lpdi->item.iSubItem, &sd)))
-                    {
-                        FIXME("failed to get details\n");
+                    if (FAILED_UNEXPECTEDLY(m_pSF2Parent->GetDetailsOf(pidl, lpdi->item.iSubItem, &sd)))
                         break;
-                    }
 
                     if (lpnmh->code == LVN_GETDISPINFOA)
                     {
@@ -1797,6 +1741,8 @@ LRESULT CDefView::OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandl
 
                 if (SUCCEEDED(m_pSFParent->GetUIObjectOf(m_hWnd, m_cidl, m_apidl, IID_NULL_PPV_ARG(IDataObject, &pda))))
                 {
+                    LPNMLISTVIEW params = (LPNMLISTVIEW)lParam;
+
                     if (SUCCEEDED(m_pSFParent->GetAttributesOf(m_cidl, m_apidl, &dwAttributes)))
                     {
                         if (dwAttributes & SFGAO_CANLINK)
@@ -1814,6 +1760,17 @@ LRESULT CDefView::OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandl
                     DWORD dwEffect2;
 
                     m_pSourceDataObject = pda;
+                    m_ptFirstMousePos = params->ptAction;
+                    ClientToScreen(&m_ptFirstMousePos);
+
+                    HIMAGELIST big_icons, small_icons;
+                    Shell_GetImageLists(&big_icons, &small_icons);
+                    PCUITEMID_CHILD pidl = _PidlByItem(params->iItem);
+                    int iIcon = SHMapPIDLToSystemImageListIndex(m_pSFParent, pidl, 0);
+                    POINT ptItem;
+                    m_ListView.GetItemPosition(params->iItem, &ptItem);
+
+                    ImageList_BeginDrag(big_icons, iIcon, params->ptAction.x - ptItem.x, params->ptAction.y - ptItem.y);
 
                     DoDragDrop(pda, this, dwEffect, &dwEffect2);
 
@@ -2001,40 +1958,35 @@ LRESULT CDefView::OnSettingChange(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL
 */
 LRESULT CDefView::OnInitMenuPopup(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
 {
-    MENUITEMINFOW mii = { 0 };
-    HMENU hSubmenu = (HMENU) wParam;
-
-    TRACE("OnInitMenuPopup lParam=%d\n", lParam);
-    
-    mii.cbSize = sizeof(mii);
-    mii.fMask = MIIM_ID | MIIM_SUBMENU;
+    HMENU hmenu = (HMENU) wParam;
+    int nPos = LOWORD(lParam);
+    UINT  menuItemId;
 
-    if (!GetMenuItemInfoW(this->m_hMenu, lParam, TRUE, &mii))
-    {
-        TRACE("OnInitMenuPopup GetMenuItemInfoW failed!\n");
-        return FALSE;
-    }
+    OnCustomItem(uMsg, wParam, lParam, bHandled);
 
-    UINT  menuItemId = mii.wID;
+    HMENU hViewMenu = GetSubmenuByID(m_hMenu, FCIDM_MENU_VIEW);
 
-    if (mii.hSubMenu != hSubmenu)
-    {
-        TRACE("OnInitMenuPopup submenu does not match!!!!\n");
+    /* Lets try to find out what the hell wParam is */
+    if (hmenu == GetSubMenu(m_hMenu, nPos))
+        menuItemId = ReallyGetMenuItemID(m_hMenu, nPos);
+    else if (hViewMenu && hmenu == GetSubMenu(hViewMenu, nPos))
+        menuItemId = ReallyGetMenuItemID(hViewMenu, nPos);
+    else if (m_hContextMenu && hmenu == GetSubMenu(m_hContextMenu, nPos))
+        menuItemId = ReallyGetMenuItemID(m_hContextMenu, nPos);
+    else
         return FALSE;
-    }
-
-    TRACE("OnInitMenuPopup id=%d\n", menuItemId);
 
     switch (menuItemId)
     {
     case FCIDM_MENU_FILE:
-        PrepareShowFileMenu(hSubmenu);
-        break;
-    case FCIDM_MENU_EDIT:
-        //PrepareShowEditMenu(hSubmenu);
+        FillFileMenu();
         break;
     case FCIDM_MENU_VIEW:
-        PrepareShowViewMenu(hSubmenu);
+    case FCIDM_SHVIEW_VIEW:
+        CheckViewMode(hmenu);
+        break;
+    case FCIDM_SHVIEW_ARRANGE:
+        FillArrangeAsMenu(hmenu);
         break;
     }
 
@@ -2164,10 +2116,10 @@ HRESULT WINAPI CDefView::DestroyViewWindow()
         m_hAccel = NULL;
     }
 
-    if (m_hView)
+    if (m_hMenuViewModes)
     {
-        DestroyMenu(m_hView);
-        m_hView = NULL;
+        DestroyMenu(m_hMenuViewModes);
+        m_hMenuViewModes = NULL;
     }
 
     if (m_hMenu)
@@ -2257,7 +2209,7 @@ HRESULT WINAPI CDefView::SelectItem(PCUITEMID_CHILD pidl, UINT uFlags)
         lvItem.iItem++;
     }
 
-    if(uFlags & SVSI_EDIT)
+    if((uFlags & SVSI_EDIT) == SVSI_EDIT)
         m_ListView.EditLabel(i);
 
     return S_OK;
@@ -2276,15 +2228,13 @@ HRESULT WINAPI CDefView::GetItemObject(UINT uItem, REFIID riid, LPVOID *ppvOut)
         case SVGIO_BACKGROUND:
             if (IsEqualIID(riid, IID_IContextMenu))
             {
-                //*ppvOut = ISvBgCm_Constructor(m_pSFParent, FALSE);
                 if (!ppvOut)
                     hr = E_OUTOFMEMORY;
 
-                IContextMenu* pcm;
-                hr = CDefFolderMenu_Create2(NULL, NULL, 0, NULL, m_pSFParent, NULL, 0, NULL, &pcm);
-                if (FAILED(hr))
+                hr = CDefViewBckgrndMenu_CreateInstance(m_pSF2Parent, riid, ppvOut);
+                if (FAILED_UNEXPECTEDLY(hr))
                     return hr;
-                *ppvOut = pcm;
+
             }
             else if (IsEqualIID(riid, IID_IDispatch))
             {
@@ -2292,9 +2242,7 @@ HRESULT WINAPI CDefView::GetItemObject(UINT uItem, REFIID riid, LPVOID *ppvOut)
                 {
                     hr = CDefViewDual_Constructor(riid, (LPVOID*)&m_pShellFolderViewDual);
                     if (FAILED_UNEXPECTEDLY(hr))
-                    {
                         return hr;
-                    }
                 }
                 hr = m_pShellFolderViewDual->QueryInterface(riid, ppvOut);
             }
@@ -2303,6 +2251,8 @@ HRESULT WINAPI CDefView::GetItemObject(UINT uItem, REFIID riid, LPVOID *ppvOut)
         case SVGIO_SELECTION:
             GetSelections();
             hr = m_pSFParent->GetUIObjectOf(m_hWnd, m_cidl, m_apidl, riid, 0, ppvOut);
+            if (FAILED_UNEXPECTEDLY(hr))
+                return hr;
             break;
     }
 
@@ -2481,7 +2431,7 @@ HRESULT STDMETHODCALLTYPE CDefView::SelectItem(int iItem, DWORD dwFlags)
 
     m_ListView.SetItemState(iItem, lvItem.state, lvItem.stateMask);
 
-    if (dwFlags & SVSI_EDIT)
+    if ((dwFlags & SVSI_EDIT) == SVSI_EDIT)
         m_ListView.EditLabel(iItem);
 
     return S_OK;
@@ -2834,25 +2784,20 @@ HRESULT WINAPI CDefView::Exec(const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCm
             if (V_VT(pvaIn) != VT_INT_PTR)
                 return OLECMDERR_E_NOTSUPPORTED;
 
-
             TPMPARAMS params;
             params.cbSize = sizeof(params);
             params.rcExclude = *(RECT*) V_INTREF(pvaIn);
 
-            HMENU hView = m_hView;
-#if 0
-            hView = CreatePopupMenu();
-            AppendMenuW(hView, MF_STRING, FCIDM_SHVIEW_BIGICON, L"Big!");
-            AppendMenuW(hView, MF_STRING, FCIDM_SHVIEW_SMALLICON, L"Small!");
-            AppendMenuW(hView, MF_STRING, FCIDM_SHVIEW_LISTVIEW, L"List!");
-            AppendMenuW(hView, MF_STRING, FCIDM_SHVIEW_REPORTVIEW, L"Report!");
-#endif
-
-            if (hView)
+            if (m_hMenuViewModes)
             {
-                PrepareShowViewMenu(hView);
-
-                TrackPopupMenuEx(hView, TPM_LEFTALIGN | TPM_TOPALIGN, params.rcExclude.left, params.rcExclude.bottom, m_hWndParent, &params);
+                /* Duplicate all but the last two items of the view modes menu */
+                HMENU hmenuViewPopup = CreatePopupMenu();
+                Shell_MergeMenus(hmenuViewPopup, m_hMenuViewModes, 0, 0, 0xFFFF, 0);
+                DeleteMenu(hmenuViewPopup, GetMenuItemCount(hmenuViewPopup) - 1, MF_BYPOSITION);
+                DeleteMenu(hmenuViewPopup, GetMenuItemCount(hmenuViewPopup) - 1, MF_BYPOSITION);
+                CheckViewMode(hmenuViewPopup);
+                TrackPopupMenuEx(hmenuViewPopup, TPM_LEFTALIGN | TPM_TOPALIGN, params.rcExclude.left, params.rcExclude.bottom, m_hWndParent, &params);
+                ::DestroyMenu(hmenuViewPopup);
             }
 
             // pvaOut is VT_I4 with value 0x403 (cmd id of the new mode maybe?)
@@ -2889,17 +2834,14 @@ HRESULT WINAPI CDefView::Exec(const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCm
 
 HRESULT CDefView::drag_notify_subitem(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
 {
-    LVHITTESTINFO htinfo;
     LONG lResult;
     HRESULT hr;
     RECT clientRect;
 
     /* Map from global to client coordinates and query the index of the listview-item, which is
      * currently under the mouse cursor. */
-    htinfo.pt.x = pt.x;
-    htinfo.pt.y = pt.y;
-    htinfo.flags = LVHT_ONITEM;
-    ::ScreenToClient(m_ListView, &htinfo.pt);
+    LVHITTESTINFO htinfo = {{pt.x, pt.y}, LVHT_ONITEM};
+    ScreenToClient(&htinfo.pt);
     lResult = m_ListView.HitTest(&htinfo);
 
     /* Send WM_*SCROLL messages every 250 ms during drag-scrolling */
@@ -2932,6 +2874,22 @@ HRESULT CDefView::drag_notify_subitem(DWORD grfKeyState, POINTL pt, DWORD *pdwEf
 
     m_ptLastMousePos = htinfo.pt;
 
+    /* We need to check if we drag the selection over itself */
+    if (lResult != -1 && m_pSourceDataObject.p != NULL)
+    {
+        PCUITEMID_CHILD pidl = _PidlByItem(lResult);
+
+        for (UINT i = 0; i < m_cidl; i++)
+        {
+            if (pidl == m_apidl[i])
+            {
+                /* The item that is being draged is hovering itself. */
+                lResult = -1;
+                break;
+            }
+        }
+    }
+
     /* If we are still over the previous sub-item, notify it via DragOver and return. */
     if (m_pCurDropTarget && lResult == m_iDragOverItem)
         return m_pCurDropTarget->DragOver(grfKeyState, pt, pdwEffect);
@@ -2939,11 +2897,16 @@ HRESULT CDefView::drag_notify_subitem(DWORD grfKeyState, POINTL pt, DWORD *pdwEf
     /* We've left the previous sub-item, notify it via DragLeave and Release it. */
     if (m_pCurDropTarget)
     {
+        PCUITEMID_CHILD pidl = _PidlByItem(m_iDragOverItem);
+        if (pidl)
+            SelectItem(pidl, 0);
+
         m_pCurDropTarget->DragLeave();
         m_pCurDropTarget.Release();
     }
 
     m_iDragOverItem = lResult;
+
     if (lResult == -1)
     {
         /* We are not above one of the listview's subitems. Bind to the parent folder's
@@ -2962,7 +2925,15 @@ HRESULT CDefView::drag_notify_subitem(DWORD grfKeyState, POINTL pt, DWORD *pdwEf
 
     /* If anything failed, m_pCurDropTarget should be NULL now, which ought to be a save state. */
     if (FAILED(hr))
+    {
+        *pdwEffect = DROPEFFECT_NONE;
         return hr;
+    }
+
+    if (m_iDragOverItem != -1)
+    {
+        SelectItem(m_iDragOverItem, SVSI_SELECT);
+    }
 
     /* Notify the item just entered via DragEnter. */
     return m_pCurDropTarget->DragEnter(m_pCurDataObject, grfKeyState, pt, pdwEffect);
@@ -2972,18 +2943,30 @@ HRESULT WINAPI CDefView::DragEnter(IDataObject *pDataObject, DWORD grfKeyState,
 {
     /* Get a hold on the data object for later calls to DragEnter on the sub-folders */
     m_pCurDataObject = pDataObject;
-    pDataObject->AddRef();
 
-    return drag_notify_subitem(grfKeyState, pt, pdwEffect);
+    HRESULT hr = drag_notify_subitem(grfKeyState, pt, pdwEffect);    
+    if (SUCCEEDED(hr))
+    {
+        POINT ptClient = {pt.x, pt.y};
+        ScreenToClient(&ptClient);
+        ImageList_DragEnter(m_hWnd, ptClient.x, ptClient.y);
+    }
+
+    return hr;
 }
 
 HRESULT WINAPI CDefView::DragOver(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
 {
+    POINT ptClient = {pt.x, pt.y};
+    ScreenToClient(&ptClient);
+    ImageList_DragMove(ptClient.x, ptClient.y);
     return drag_notify_subitem(grfKeyState, pt, pdwEffect);
 }
 
 HRESULT WINAPI CDefView::DragLeave()
 {
+    ImageList_DragLeave(m_hWnd);
+
     if (m_pCurDropTarget)
     {
         m_pCurDropTarget->DragLeave();
@@ -3004,19 +2987,38 @@ HRESULT WINAPI CDefView::Drop(IDataObject* pDataObject, DWORD grfKeyState, POINT
 {
     ERR("GetKeyState(VK_LBUTTON): %d\n", GetKeyState(VK_LBUTTON));
 
-    if ((m_iDragOverItem == -1) && 
+    ImageList_DragLeave(m_hWnd);
+    ImageList_EndDrag();
+
+    if ((m_iDragOverItem == -1 || m_pCurDropTarget == NULL) && 
         (*pdwEffect & DROPEFFECT_MOVE) && 
-        (GetKeyState(VK_LBUTTON) != 0) &&
+        /*(GetKeyState(VK_LBUTTON) != 0) &&*/
         (m_pSourceDataObject.p) && 
         (SHIsSameObject(pDataObject, m_pSourceDataObject)))
     {
-        ERR("Should implement moving items here!\n");
-
         if (m_pCurDropTarget)
         {
             m_pCurDropTarget->DragLeave();
             m_pCurDropTarget.Release();
         }
+
+        /* Restore the selection */
+        m_ListView.SetItemState(-1, 0, LVIS_SELECTED);
+        for (UINT i = 0 ; i < m_cidl; i++)
+            SelectItem(m_apidl[i], SVSI_SELECT);
+
+        /* Reposition the items */
+        int lvIndex = -1;
+        while ((lvIndex = m_ListView.GetNextItem(lvIndex,  LVNI_SELECTED)) > -1)
+        {
+            POINT ptItem;
+            if (m_ListView.GetItemPosition(lvIndex, &ptItem))
+            {
+                ptItem.x += pt.x - m_ptFirstMousePos.x;
+                ptItem.y += pt.y - m_ptFirstMousePos.y;
+                m_ListView.SetItemPosition(lvIndex, &ptItem);
+            }
+        }
     }
     else if (m_pCurDropTarget)
     {
@@ -3156,7 +3158,7 @@ HRESULT CDefView::_MergeToolbar()
  */
 HRESULT WINAPI IShellView_Constructor(IShellFolder *pFolder, IShellView **newView)
 {
-    return ShellObjectCreatorInit<CDefView>(pFolder, IID_IShellView, newView);
+    return ShellObjectCreatorInit<CDefView>(pFolder, IID_PPV_ARG(IShellView, newView));
 }
 
 HRESULT WINAPI CDefView_Constructor(IShellFolder *pFolder, REFIID riid, LPVOID * ppvOut)