[RSHELL]
[reactos.git] / base / shell / rshell / CMenuSite.cpp
index 4e901b8..a90c89b 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
  */
-
 #include "precomp.h"
 #include <atlwin.h>
 #include <shlwapi_undoc.h>
 
-WINE_DEFAULT_DEBUG_CHANNEL(menusite);
+#include "CMenuSite.h"
 
-bool _assert(bool cond, LPCSTR expr, LPCSTR file, DWORD line, LPCSTR func)
-{
-#if DBG
-    if (!cond)
-    {
-        wine_dbg_printf("%s(%d): Assertion failed '%s', at %s", file, line, expr, func);
-        DebugBreak();
-    }
-#endif
-    return cond;
-}
-#define DBGASSERT(x) _assert(!!(x), #x, __FILE__, __LINE__, __FUNCSIG__)
-
-class CMenuSite :
-    public CComObjectRootEx<CComMultiThreadModelNoCS>,
-    public CWindowImpl<CMenuSite, CWindow, CControlWinTraits>,
-    public IBandSite,
-    public IDeskBarClient,
-    public IOleCommandTarget,
-    public IInputObject,
-    public IInputObjectSite,
-    public IWinEventHandler,
-    public IServiceProvider
-{
-    CComPtr<IUnknown>         m_DeskBarSite;
-    CComPtr<IUnknown>         m_BandObject;
-    CComPtr<IDeskBand>        m_DeskBand;
-    CComPtr<IWinEventHandler> m_WinEventHandler;
-    HWND                      m_hWndBand;
-
-public:
-    CMenuSite();
-    ~CMenuSite() {}
-
-    DECLARE_WND_CLASS_EX(_T("MenuSite"), 0, COLOR_WINDOW)
-
-    DECLARE_NOT_AGGREGATABLE(CMenuSite)
-    DECLARE_PROTECT_FINAL_CONSTRUCT()
-    BEGIN_COM_MAP(CMenuSite)
-        COM_INTERFACE_ENTRY_IID(IID_IBandSite, IBandSite)
-        COM_INTERFACE_ENTRY_IID(IID_IDeskBarClient, IDeskBarClient)
-        COM_INTERFACE_ENTRY_IID(IID_IOleWindow, IOleWindow)
-        COM_INTERFACE_ENTRY_IID(IID_IOleCommandTarget, IOleCommandTarget)
-        COM_INTERFACE_ENTRY_IID(IID_IInputObject, IInputObject)
-        COM_INTERFACE_ENTRY_IID(IID_IInputObjectSite, IInputObjectSite)
-        COM_INTERFACE_ENTRY_IID(IID_IWinEventHandler, IWinEventHandler)
-        COM_INTERFACE_ENTRY_IID(IID_IServiceProvider, IServiceProvider)
-    END_COM_MAP()
-
-    // IBandSite
-    virtual HRESULT STDMETHODCALLTYPE AddBand(IUnknown * punk);
-    virtual HRESULT STDMETHODCALLTYPE EnumBands(UINT uBand, DWORD* pdwBandID);
-    virtual HRESULT STDMETHODCALLTYPE QueryBand(DWORD dwBandID, IDeskBand **ppstb, DWORD *pdwState, LPWSTR pszName, int cchName);
-    virtual HRESULT STDMETHODCALLTYPE GetBandObject(DWORD dwBandID, REFIID riid, VOID **ppv);
-
-    // IDeskBarClient
-    virtual HRESULT STDMETHODCALLTYPE SetDeskBarSite(IUnknown *punkSite);
-    virtual HRESULT STDMETHODCALLTYPE GetSize(DWORD dwWhich, LPRECT prc);
-    virtual HRESULT STDMETHODCALLTYPE UIActivateDBC(DWORD dwState);
-
-    // IOleWindow
-    virtual HRESULT STDMETHODCALLTYPE GetWindow(HWND *phwnd);
-
-    // IOleCommandTarget
-    virtual HRESULT STDMETHODCALLTYPE QueryStatus(const GUID * pguidCmdGroup, ULONG cCmds, OLECMD prgCmds[], OLECMDTEXT *pCmdText);
-    virtual HRESULT STDMETHODCALLTYPE Exec(const GUID * pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut);
-
-    // IInputObject
-    virtual HRESULT STDMETHODCALLTYPE UIActivateIO(BOOL fActivate, LPMSG lpMsg);
-    virtual HRESULT STDMETHODCALLTYPE HasFocusIO();
-    virtual HRESULT STDMETHODCALLTYPE TranslateAcceleratorIO(LPMSG lpMsg);
-
-    // IInputObjectSite
-    virtual HRESULT STDMETHODCALLTYPE OnFocusChangeIS(IUnknown *punkObj, BOOL fSetFocus);
-
-    // IWinEventHandler
-    virtual HRESULT STDMETHODCALLTYPE IsWindowOwner(HWND hWnd);
-    virtual HRESULT STDMETHODCALLTYPE OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *theResult);
-
-    // IServiceProvider
-    virtual HRESULT STDMETHODCALLTYPE QueryService(REFGUID guidService, REFIID riid, void **ppvObject);
-
-
-    // Using custom message map instead 
-    virtual BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT &lResult, DWORD mapId = 0);
-
-    // UNIMPLEMENTED
-    virtual HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(BOOL fEnterMode);
-    virtual HRESULT STDMETHODCALLTYPE GetBandSiteInfo(BANDSITEINFO *pbsinfo);
-    virtual HRESULT STDMETHODCALLTYPE RemoveBand(DWORD dwBandID);
-    virtual HRESULT STDMETHODCALLTYPE SetBandSiteInfo(const BANDSITEINFO *pbsinfo);
-    virtual HRESULT STDMETHODCALLTYPE SetBandState(DWORD dwBandID, DWORD dwMask, DWORD dwState);
-    virtual HRESULT STDMETHODCALLTYPE SetModeDBC(DWORD dwMode);
-
-private:
-    IUnknown * ToIUnknown() { return (IDeskBarClient*)this; }
-};
+WINE_DEFAULT_DEBUG_CHANNEL(menusite);
 
 extern "C"
-HRESULT CMenuSite_Constructor(REFIID riid, LPVOID *ppv)
+HRESULT WINAPI CMenuSite_Constructor(REFIID riid, LPVOID *ppv)
 {
     *ppv = NULL;
 
@@ -134,7 +37,7 @@ HRESULT CMenuSite_Constructor(REFIID riid, LPVOID *ppv)
 
     HRESULT hr = site->QueryInterface(riid, ppv);
 
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         site->Release();
 
     return hr;
@@ -196,32 +99,51 @@ HRESULT STDMETHODCALLTYPE CMenuSite::OnFocusChangeIS(IUnknown *punkObj, BOOL fSe
 
 HRESULT STDMETHODCALLTYPE CMenuSite::AddBand(IUnknown * punk)
 {
-    if (SHIsSameObject(punk, m_BandObject))
-        return S_OK + 0;
+    HRESULT hr;
+
+#define TO_HRESULT(x) ((HRESULT)(S_OK+(x)))
 
-    IUnknown_SetSite(m_BandObject, NULL);
+    CComPtr<IUnknown> pUnknown;
 
-    BOOL result = m_hWndBand != NULL;
+    punk->QueryInterface(IID_PPV_ARG(IUnknown, &pUnknown));
+
+    if (pUnknown == m_BandObject)
+        return TO_HRESULT(0);
+
+    if (m_BandObject)
+    {
+        hr = IUnknown_SetSite(m_BandObject, NULL);
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+    }
 
     m_BandObject = NULL;
     m_DeskBand = NULL;
     m_WinEventHandler = NULL;
     m_hWndBand = NULL;
 
-    if (!punk)
-        return result ? S_OK + 0 : E_FAIL;
+    if (!pUnknown)
+        return TO_HRESULT(0);
+
+    hr = pUnknown->QueryInterface(IID_PPV_ARG(IDeskBand, &m_DeskBand));
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
 
-    DBGASSERT(SUCCEEDED(punk->QueryInterface(IID_PPV_ARG(IDeskBand, &m_DeskBand))));
-    DBGASSERT(SUCCEEDED(punk->QueryInterface(IID_PPV_ARG(IWinEventHandler, &m_WinEventHandler))));
+    hr = pUnknown->QueryInterface(IID_PPV_ARG(IWinEventHandler, &m_WinEventHandler));
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
 
-    IUnknown_SetSite(punk, this->ToIUnknown());
-    IUnknown_GetWindow(punk, &m_hWndBand);
+    hr = IUnknown_SetSite(pUnknown, this->ToIUnknown());
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
 
-    m_BandObject = punk;
+    hr = IUnknown_GetWindow(pUnknown, &m_hWndBand);
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
 
-    punk->AddRef();
+    m_BandObject = pUnknown;
 
-    return S_OK + 0;
+    return TO_HRESULT(0);
 }
 
 HRESULT STDMETHODCALLTYPE CMenuSite::EnumBands(UINT uBand, DWORD* pdwBandID)
@@ -241,7 +163,7 @@ HRESULT STDMETHODCALLTYPE CMenuSite::Exec(const GUID * pguidCmdGroup, DWORD nCmd
 
 HRESULT STDMETHODCALLTYPE CMenuSite::GetBandObject(DWORD dwBandID, REFIID riid, VOID **ppv)
 {
-    if (!DBGASSERT(dwBandID == 0) || m_BandObject == NULL)
+    if (dwBandID != 0 || m_BandObject == NULL)
     {
         *ppv = NULL;
         return E_NOINTERFACE;
@@ -273,7 +195,8 @@ HRESULT STDMETHODCALLTYPE CMenuSite::GetSize(DWORD dwWhich, LPRECT prc)
 
 HRESULT STDMETHODCALLTYPE CMenuSite::GetWindow(HWND *phwnd)
 {
-    DBGASSERT(IsWindow());
+    if (!IsWindow())
+        return E_FAIL;
 
     *phwnd = m_hWnd;
 
@@ -301,8 +224,8 @@ HRESULT STDMETHODCALLTYPE CMenuSite::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wPa
 
 HRESULT STDMETHODCALLTYPE CMenuSite::QueryBand(DWORD dwBandID, IDeskBand **ppstb, DWORD *pdwState, LPWSTR pszName, int cchName)
 {
-    DBGASSERT(dwBandID == 0);
-    DBGASSERT(!IsBadWritePtr(ppstb, sizeof(*ppstb)));
+    if (dwBandID != 0)
+        return E_FAIL;
 
     if (!m_BandObject)
     {
@@ -329,20 +252,22 @@ HRESULT STDMETHODCALLTYPE CMenuSite::QueryService(REFGUID guidService, REFIID ri
         IsEqualGUID(guidService, SID_SMenuBandChild))
     {
         if (m_BandObject == NULL)
-            return E_FAIL;
+            return E_NOINTERFACE;
 
         return IUnknown_QueryService(m_BandObject, guidService, riid, ppvObject);
     }
 
-    DBGASSERT(m_DeskBarSite);
+    if (!m_DeskBarSite)
+        return E_NOINTERFACE;
 
     return IUnknown_QueryService(m_DeskBarSite, guidService, riid, ppvObject);
 }
 
 HRESULT STDMETHODCALLTYPE CMenuSite::QueryStatus(const GUID * pguidCmdGroup, ULONG cCmds, OLECMD prgCmds [], OLECMDTEXT *pCmdText)
 {
-    if (!DBGASSERT(m_DeskBarSite))
+    if (!m_DeskBarSite)
         return E_FAIL;
+
     return IUnknown_QueryStatus(m_DeskBarSite, *pguidCmdGroup, cCmds, prgCmds, pCmdText);
 }
 
@@ -378,7 +303,7 @@ HRESULT STDMETHODCALLTYPE CMenuSite::SetDeskBarSite(IUnknown *punkSite)
         m_DeskBand->CloseDW(0);
     }
 
-    IUnknown_SetSite(m_BandObject, NULL);
+    hr = IUnknown_SetSite(m_BandObject, NULL);
 
     m_BandObject = NULL;
     m_DeskBand = NULL;
@@ -421,6 +346,7 @@ BOOL CMenuSite::ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
             {
                 RECT Rect = { 0 };
                 GetClientRect(&Rect);
+                Rect.right = Rect.right;
                 pMenuPopup->OnPosRectChangeDB(&Rect);
             }
         }
@@ -428,7 +354,7 @@ BOOL CMenuSite::ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
         lResult = 1;
         break;
     case WM_NOTIFY:
-        hWndTarget = ((NMHDR *)lParam)->hwndFrom;
+        hWndTarget = reinterpret_cast<LPNMHDR>(lParam)->hwndFrom;
         break;
     case WM_COMMAND:
         hWndTarget = (HWND) lParam;