[SHELL32] Add CDefView::DefMessageSFVCB (Retrial of #1145) (#1151)
[reactos.git] / dll / win32 / shell32 / CDefView.cpp
index 280be96..b382dcd 100644 (file)
@@ -72,6 +72,7 @@ class CDefView :
     private:
         CComPtr<IShellFolder>     m_pSFParent;
         CComPtr<IShellFolder2>    m_pSF2Parent;
+        CComPtr<IShellFolderViewCB> m_pShellFolderViewCB;
         CComPtr<IShellBrowser>    m_pShellBrowser;
         CComPtr<ICommDlgBrowser>  m_pCommDlgBrowser;
         CComPtr<IShellFolderViewDual> m_pShellFolderViewDual;
@@ -113,6 +114,7 @@ class CDefView :
     private:
         HRESULT _MergeToolbar();
         BOOL _Sort();
+        HRESULT _DoFolderViewCB(UINT uMsg, WPARAM wParam, LPARAM lParam);
 
     public:
         CDefView();
@@ -123,10 +125,10 @@ class CDefView :
         HRESULT OnStateChange(UINT uFlags);
         void UpdateStatusbar();
         void CheckToolbar();
-        void SetStyle(DWORD dwAdd, DWORD dwRemove);
         BOOL CreateList();
         void UpdateListColors();
         BOOL InitList();
+        HRESULT DefMessageSFVCB(UINT uMsg, WPARAM wParam, LPARAM lParam);
         static INT CALLBACK ListViewCompareItems(LPARAM lParam1, LPARAM lParam2, LPARAM lpData);
 
         PCUITEMID_CHILD _PidlByItem(int i);
@@ -416,7 +418,7 @@ HRESULT CDefView::IncludeObject(PCUITEMID_CHILD pidl)
     {
         TRACE("ICommDlgBrowser::IncludeObject pidl=%p\n", pidl);
         ret = m_pCommDlgBrowser->IncludeObject(this, pidl);
-        TRACE("--0x%08x\n", ret);
+        TRACE("-- returns 0x%08x\n", ret);
     }
 
     return ret;
@@ -430,7 +432,7 @@ HRESULT CDefView::OnDefaultCommand()
     {
         TRACE("ICommDlgBrowser::OnDefaultCommand\n");
         ret = m_pCommDlgBrowser->OnDefaultCommand(this);
-        TRACE("-- returns %08x\n", ret);
+        TRACE("-- returns 0x%08x\n", ret);
     }
 
     return ret;
@@ -498,18 +500,6 @@ void CDefView::UpdateStatusbar()
  *
  * ##### helperfunctions for initializing the view #####
  */
-/**********************************************************
- *    change the style of the listview control
- */
-void CDefView::SetStyle(DWORD dwAdd, DWORD dwRemove)
-{
-    DWORD tmpstyle;
-
-    TRACE("(%p)\n", this);
-
-    tmpstyle = ::GetWindowLongPtrW(m_ListView, GWL_STYLE);
-    ::SetWindowLongPtrW(m_ListView, GWL_STYLE, dwAdd | (tmpstyle & ~dwRemove));
-}
 
 /**********************************************************
 * ShellView_CreateList()
@@ -518,7 +508,9 @@ void CDefView::SetStyle(DWORD dwAdd, DWORD dwRemove)
 */
 BOOL CDefView::CreateList()
 {
+    HRESULT hr;
     DWORD dwStyle, dwExStyle;
+    UINT ViewMode;
 
     TRACE("%p\n", this);
 
@@ -531,6 +523,16 @@ BOOL CDefView::CreateList()
     else
         dwStyle |= LVS_ALIGNTOP | LVS_SHOWSELALWAYS;
 
+    ViewMode = m_FolderSettings.ViewMode;
+    hr = _DoFolderViewCB(SFVM_DEFVIEWMODE, NULL, (LPARAM)&ViewMode);
+    if (SUCCEEDED(hr))
+    {
+        if (ViewMode >= FVM_FIRST && ViewMode <= FVM_LAST)
+            m_FolderSettings.ViewMode = ViewMode;
+        else
+            ERR("Ignoring invalid ViewMode from SFVM_DEFVIEWMODE: %u (was: %u)\n", ViewMode, m_FolderSettings.ViewMode);
+    }
+
     switch (m_FolderSettings.ViewMode)
     {
         case FVM_ICON:
@@ -951,6 +953,8 @@ HRESULT CDefView::FillList()
     /*turn the listview's redrawing back on and force it to draw*/
     m_ListView.SetRedraw(TRUE);
 
+    _DoFolderViewCB(SFVM_LISTREFRESHED, NULL, NULL);
+
     return S_OK;
 }
 
@@ -1059,6 +1063,8 @@ LRESULT CDefView::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandl
         m_hNotify = SHChangeNotifyRegister(m_hWnd, SHCNRF_InterruptLevel | SHCNRF_ShellLevel, SHCNE_ALLEVENTS, SHV_CHANGE_NOTIFY, 1, &ntreg);
     }
 
+    /* _DoFolderViewCB(SFVM_GETNOTIFY, ??  ??) */
+
     m_hAccel = LoadAcceleratorsW(shell32_hInstance, MAKEINTRESOURCEW(IDA_SHELLVIEW));
 
     UpdateStatusbar();
@@ -1174,6 +1180,19 @@ HRESULT CDefView::FillArrangeAsMenu(HMENU hmenuArrange)
                        m_sortInfo.nHeaderID + 0x30, 
                        MF_BYCOMMAND);
 
+    if (m_FolderSettings.ViewMode == FVM_DETAILS || m_FolderSettings.ViewMode == FVM_LIST)
+    {
+        EnableMenuItem(hmenuArrange, FCIDM_SHVIEW_AUTOARRANGE, MF_BYCOMMAND | MF_GRAYED);
+    }
+    else
+    {
+        EnableMenuItem(hmenuArrange, FCIDM_SHVIEW_AUTOARRANGE, MF_BYCOMMAND); 
+
+        if (GetAutoArrange() == S_OK)
+            CheckMenuItem(hmenuArrange, FCIDM_SHVIEW_AUTOARRANGE, MF_CHECKED);
+    }
+
+
     return S_OK;
 }
 
@@ -1410,6 +1429,8 @@ LRESULT CDefView::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled
         ::MoveWindow(m_ListView, 0, 0, wWidth, wHeight, TRUE);
     }
 
+    _DoFolderViewCB(SFVM_SIZE, 0, 0);
+
     return 0;
 }
 
@@ -1535,25 +1556,25 @@ LRESULT CDefView::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHand
     {
         case FCIDM_SHVIEW_SMALLICON:
             m_FolderSettings.ViewMode = FVM_SMALLICON;
-            SetStyle (LVS_SMALLICON, LVS_TYPEMASK);
+            m_ListView.ModifyStyle(LVS_TYPEMASK, LVS_SMALLICON);
             CheckToolbar();
             break;
 
         case FCIDM_SHVIEW_BIGICON:
             m_FolderSettings.ViewMode = FVM_ICON;
-            SetStyle (LVS_ICON, LVS_TYPEMASK);
+            m_ListView.ModifyStyle(LVS_TYPEMASK, LVS_ICON);
             CheckToolbar();
             break;
 
         case FCIDM_SHVIEW_LISTVIEW:
             m_FolderSettings.ViewMode = FVM_LIST;
-            SetStyle (LVS_LIST, LVS_TYPEMASK);
+            m_ListView.ModifyStyle(LVS_TYPEMASK, LVS_LIST);
             CheckToolbar();
             break;
 
         case FCIDM_SHVIEW_REPORTVIEW:
             m_FolderSettings.ViewMode = FVM_DETAILS;
-            SetStyle (LVS_REPORT, LVS_TYPEMASK);
+            m_ListView.ModifyStyle(LVS_TYPEMASK, LVS_REPORT);
             CheckToolbar();
             break;
 
@@ -1568,9 +1589,14 @@ LRESULT CDefView::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHand
             break;
 
         case FCIDM_SHVIEW_SNAPTOGRID:
-        case FCIDM_SHVIEW_AUTOARRANGE:
             //FIXME
             break;
+        case FCIDM_SHVIEW_AUTOARRANGE:
+            if (GetAutoArrange() == S_OK)
+                m_ListView.ModifyStyle(LVS_AUTOARRANGE, 0);
+            else
+                AutoArrange();
+            break;
         case FCIDM_SHVIEW_SELECTALL:
             m_ListView.SetItemState(-1, LVIS_SELECTED, LVIS_SELECTED);
             break;
@@ -1757,6 +1783,7 @@ LRESULT CDefView::OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandl
             TRACE("-- LVN_ITEMCHANGED %p\n", this);
             OnStateChange(CDBOSC_SELCHANGE);  /* the browser will get the IDataObject now */
             UpdateStatusbar();
+            _DoFolderViewCB(SFVM_SELECTIONCHANGED, NULL/* FIXME */, NULL/* FIXME */);
             break;
 
         case LVN_BEGINDRAG:
@@ -1834,7 +1861,7 @@ LRESULT CDefView::OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandl
                 LVITEMW lvItem;
 
                 pidl = _PidlByItem(lpdi->item);
-                PITEMID_CHILD pidlNew;
+                PITEMID_CHILD pidlNew = NULL;
                 hr = m_pSFParent->SetNameOf(0, pidl, lpdi->item.pszText, SHGDN_INFOLDER, &pidlNew);
 
                 if (SUCCEEDED(hr) && pidlNew)
@@ -2073,7 +2100,7 @@ HRESULT WINAPI CDefView::TranslateAccelerator(LPMSG lpmsg)
         if (::TranslateAcceleratorW(m_hWnd, m_hAccel, lpmsg) != 0)
             return S_OK;
 
-        TRACE("-- key=0x04%lx\n", lpmsg->wParam) ;
+        TRACE("-- key=0x%04lx\n", lpmsg->wParam) ;
     }
 
     return m_pShellBrowser->TranslateAcceleratorSB(lpmsg, 0);
@@ -2354,7 +2381,7 @@ HRESULT STDMETHODCALLTYPE CDefView::SetCurrentViewMode(UINT ViewMode)
         }
     }
 
-    SetStyle(dwStyle, LVS_TYPEMASK);
+    m_ListView.ModifyStyle(LVS_TYPEMASK, dwStyle);
 
     /* This will not necessarily be the actual mode set above.
        This mimics the behavior of Windows XP. */
@@ -2455,7 +2482,7 @@ HRESULT STDMETHODCALLTYPE CDefView::GetDefaultSpacing(POINT *ppt)
 
 HRESULT STDMETHODCALLTYPE CDefView::GetAutoArrange()
 {
-    return E_NOTIMPL;
+    return ((m_ListView.GetStyle() & LVS_AUTOARRANGE) ? S_OK : S_FALSE);
 }
 
 HRESULT STDMETHODCALLTYPE CDefView::SelectItem(int iItem, DWORD dwFlags)
@@ -2588,6 +2615,8 @@ HRESULT STDMETHODCALLTYPE CDefView::CreateViewWindow3(IShellBrowser *psb, IShell
     if (!*hwnd)
         return E_FAIL;
 
+    _DoFolderViewCB(SFVM_WINDOWCREATED, (WPARAM)m_hWnd, NULL);
+
     SetWindowPos(HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
     UpdateWindow();
 
@@ -2638,8 +2667,9 @@ HRESULT STDMETHODCALLTYPE CDefView::ArrangeGrid()
 
 HRESULT STDMETHODCALLTYPE CDefView::AutoArrange()
 {
-    FIXME("(%p) stub\n", this);
-    return E_NOTIMPL;
+    m_ListView.ModifyStyle(0, LVS_AUTOARRANGE);
+    m_ListView.Arrange(LVA_DEFAULT);
+    return S_OK;
 }
 
 HRESULT STDMETHODCALLTYPE CDefView::AddObject(PITEMID_CHILD pidl, UINT *item)
@@ -2796,8 +2826,11 @@ HRESULT STDMETHODCALLTYPE CDefView::GetItemSpacing(ITEMSPACING *spacing)
 
 HRESULT STDMETHODCALLTYPE CDefView::SetCallback(IShellFolderViewCB  *new_cb, IShellFolderViewCB **old_cb)
 {
-    FIXME("(%p)->(%p %p) stub\n", this, new_cb, old_cb);
-    return E_NOTIMPL;
+    if (old_cb)
+        *old_cb = m_pShellFolderViewCB.Detach();
+
+    m_pShellFolderViewCB = new_cb;
+    return S_OK;
 }
 
 HRESULT STDMETHODCALLTYPE CDefView::Select(UINT flags)
@@ -3066,8 +3099,6 @@ HRESULT WINAPI CDefView::DragLeave()
 
 HRESULT WINAPI CDefView::Drop(IDataObject* pDataObject, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
 {
-    ERR("GetKeyState(VK_LBUTTON): %d\n", GetKeyState(VK_LBUTTON));
-
     ImageList_DragLeave(m_hWnd);
     ImageList_EndDrag();
 
@@ -3167,7 +3198,7 @@ HRESULT WINAPI CDefView::Unfreeze(DWORD dwFreeze)
 
 HRESULT WINAPI CDefView::SetAdvise(DWORD aspects, DWORD advf, IAdviseSink *pAdvSink)
 {
-    FIXME("partial stub: %p %08x %08x %p\n", this, aspects, advf, pAdvSink);
+    FIXME("partial stub: %p 0x%08x 0x%08x %p\n", this, aspects, advf, pAdvSink);
 
     /* FIXME: we set the AdviseSink, but never use it to send any advice */
     m_pAdvSink = pAdvSink;
@@ -3233,6 +3264,31 @@ HRESULT CDefView::_MergeToolbar()
     return S_OK;
 }
 
+// The default processing of IShellFolderView callbacks
+HRESULT CDefView::DefMessageSFVCB(UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    // TODO: SFVM_GET_CUSTOMVIEWINFO, SFVM_WINDOWCREATED
+    TRACE("CDefView::DefMessageSFVCB uMsg=%u\n", uMsg);
+    return E_NOTIMPL;
+}
+
+HRESULT CDefView::_DoFolderViewCB(UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    HRESULT hr = E_NOTIMPL;
+
+    if (m_pShellFolderViewCB)
+    {
+        hr = m_pShellFolderViewCB->MessageSFVCB(uMsg, wParam, lParam);
+    }
+
+    if (hr == E_NOTIMPL)
+    {
+        hr = DefMessageSFVCB(uMsg, wParam, lParam);
+    }
+
+    return hr;
+}
+
 HRESULT CDefView_CreateInstance(IShellFolder *pFolder, REFIID riid, LPVOID * ppvOut)
 {
     return ShellObjectCreatorInit<CDefView>(pFolder, riid, ppvOut);
@@ -3275,6 +3331,15 @@ HRESULT WINAPI SHCreateShellFolderView(const SFV_CREATE *pcsfv,
     if (FAILED(hRes))
         return hRes;
 
+    if (pcsfv->psfvcb)
+    {
+        CComPtr<IShellFolderView> sfv;
+        if (SUCCEEDED(psv->QueryInterface(IID_PPV_ARG(IShellFolderView, &sfv))))
+        {
+            sfv->SetCallback(pcsfv->psfvcb, NULL);
+        }
+    }
+
     *ppsv = psv.Detach();
     return hRes;
 }