[RSHELL]
[reactos.git] / base / shell / rshell / CMenuDeskBar.cpp
index fc5b1d7..94340cd 100644 (file)
 /*
-* Shell Menu Desk Bar
-*
-* Copyright 2014 David Quintana
-*
-* This library is free software; you can redistribute it and/or
-* modify it under the terms of the GNU Lesser General Public
-* License as published by the Free Software Foundation; either
-* version 2.1 of the License, or (at your option) any later version.
-*
-* This library is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-* Lesser General Public License for more details.
-*
-* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
-*/
+ * Shell Menu Desk Bar
+ *
+ * Copyright 2014 David Quintana
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
 #include "precomp.h"
 #include <atlwin.h>
 #include <shlwapi_undoc.h>
 
+#include "CMenuDeskBar.h"
+
 WINE_DEFAULT_DEBUG_CHANNEL(CMenuDeskBar);
 
 const static GUID CGID_MenuDeskBar = { 0x5C9F0A12, 0x959E, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x08, 0x26, 0x36 } };
 
-typedef CWinTraits<
-    WS_POPUP | WS_DLGFRAME | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
-    WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_PALETTEWINDOW
-> CMenuWinTraits;
-
-class CMenuDeskBar :
-    public CWindowImpl<CMenuDeskBar, CWindow, CMenuWinTraits>,
-    public CComCoClass<CMenuDeskBar>,
-    public CComObjectRootEx<CComMultiThreadModelNoCS>,
-    public IOleCommandTarget,
-    public IServiceProvider,
-    public IInputObjectSite,
-    public IInputObject,
-    public IMenuPopup,
-    public IObjectWithSite,
-    public IBanneredBar,
-    public IInitializeObject
-{
-public:
-    CMenuDeskBar();
-    ~CMenuDeskBar();
-
-private:
-    CComPtr<IUnknown>   m_Site;
-    CComPtr<IUnknown>   m_Client;
-    CComPtr<IMenuPopup> m_SubMenuParent;
-
-    HWND m_ClientWindow;
-
-    DWORD m_IconSize;
-    HBITMAP m_Banner;
-
-public:
-    // *** IMenuPopup methods ***
-    virtual HRESULT STDMETHODCALLTYPE Popup(POINTL *ppt, RECTL *prcExclude, MP_POPUPFLAGS dwFlags);
-    virtual HRESULT STDMETHODCALLTYPE OnSelect(DWORD dwSelectType);
-    virtual HRESULT STDMETHODCALLTYPE SetSubMenu(IMenuPopup *pmp, BOOL fSet);
-
-    // *** IOleWindow methods ***
-    virtual HRESULT STDMETHODCALLTYPE GetWindow(HWND *phwnd);
-    virtual HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(BOOL fEnterMode);
-
-    // *** IObjectWithSite methods ***
-    virtual HRESULT STDMETHODCALLTYPE SetSite(IUnknown *pUnkSite);
-    virtual HRESULT STDMETHODCALLTYPE GetSite(REFIID riid, PVOID *ppvSite);
-
-    // *** IBanneredBar methods ***
-    virtual HRESULT STDMETHODCALLTYPE SetIconSize(DWORD iIcon);
-    virtual HRESULT STDMETHODCALLTYPE GetIconSize(DWORD* piIcon);
-    virtual HRESULT STDMETHODCALLTYPE SetBitmap(HBITMAP hBitmap);
-    virtual HRESULT STDMETHODCALLTYPE GetBitmap(HBITMAP* phBitmap);
-
-    // *** IInitializeObject methods ***
-    virtual HRESULT STDMETHODCALLTYPE Initialize(THIS);
-
-    // *** IOleCommandTarget methods ***
-    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);
-
-    // *** IServiceProvider methods ***
-    virtual HRESULT STDMETHODCALLTYPE QueryService(REFGUID guidService, REFIID riid, void **ppvObject);
-
-    // *** IInputObjectSite methods ***
-    virtual HRESULT STDMETHODCALLTYPE OnFocusChangeIS(LPUNKNOWN lpUnknown, BOOL bFocus);
-
-    // *** IInputObject methods ***
-    virtual HRESULT STDMETHODCALLTYPE UIActivateIO(BOOL bActivating, LPMSG lpMsg);
-    virtual HRESULT STDMETHODCALLTYPE HasFocusIO(THIS);
-    virtual HRESULT STDMETHODCALLTYPE TranslateAcceleratorIO(LPMSG lpMsg);
-
-    // *** IDeskBar methods ***
-    virtual HRESULT STDMETHODCALLTYPE SetClient(IUnknown *punkClient);
-    virtual HRESULT STDMETHODCALLTYPE GetClient(IUnknown **ppunkClient);
-    virtual HRESULT STDMETHODCALLTYPE OnPosRectChangeDB(LPRECT prc);
-
-    DECLARE_NOT_AGGREGATABLE(CMenuDeskBar)
-    DECLARE_PROTECT_FINAL_CONSTRUCT()
-
-    DECLARE_WND_CLASS_EX(_T("BaseBar"), CS_SAVEBITS | CS_DROPSHADOW, COLOR_3DFACE)
-
-    BEGIN_MSG_MAP(CMenuDeskBar)
-        MESSAGE_HANDLER(WM_SIZE, OnSize)
-        MESSAGE_HANDLER(WM_NOTIFY, OnNotify)
-        MESSAGE_HANDLER(WM_WINDOWPOSCHANGED, OnWindowPosChanged)
-        MESSAGE_HANDLER(WM_PAINT, OnPaint)
-    END_MSG_MAP()
-
-    BEGIN_COM_MAP(CMenuDeskBar)
-        COM_INTERFACE_ENTRY_IID(IID_IMenuPopup, IMenuPopup)
-        COM_INTERFACE_ENTRY_IID(IID_IOleCommandTarget, IOleCommandTarget)
-        COM_INTERFACE_ENTRY_IID(IID_IServiceProvider, IServiceProvider)
-        COM_INTERFACE_ENTRY_IID(IID_IInputObjectSite, IInputObjectSite)
-        COM_INTERFACE_ENTRY_IID(IID_IInputObject, IInputObject)
-        COM_INTERFACE_ENTRY_IID(IID_IDeskBar, IMenuPopup)
-        COM_INTERFACE_ENTRY_IID(IID_IOleWindow, IMenuPopup)
-        COM_INTERFACE_ENTRY_IID(IID_IObjectWithSite, IObjectWithSite)
-        COM_INTERFACE_ENTRY_IID(IID_IBanneredBar, IBanneredBar)
-        COM_INTERFACE_ENTRY_IID(IID_IInitializeObject, IInitializeObject)
-    END_COM_MAP()
-
-private:
-
-    // message handlers
-    LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled);
-    LRESULT OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled);
-    LRESULT OnWindowPosChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled);
-    LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled);
-};
-
 extern "C"
-HRESULT CMenuDeskBar_Constructor(REFIID riid, LPVOID *ppv)
+HRESULT WINAPI CMenuDeskBar_Constructor(REFIID riid, LPVOID *ppv)
 {
     *ppv = NULL;
 
@@ -146,7 +39,7 @@ HRESULT CMenuDeskBar_Constructor(REFIID riid, LPVOID *ppv)
 
     HRESULT hr = deskbar->QueryInterface(riid, ppv);
 
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         deskbar->Release();
 
     return hr;
@@ -154,7 +47,8 @@ HRESULT CMenuDeskBar_Constructor(REFIID riid, LPVOID *ppv)
 
 CMenuDeskBar::CMenuDeskBar() :
     m_Client(NULL),
-    m_Banner(NULL)
+    m_Banner(NULL),
+    m_Shown(FALSE)
 {
 }
 
@@ -162,6 +56,11 @@ CMenuDeskBar::~CMenuDeskBar()
 {
 }
 
+HRESULT STDMETHODCALLTYPE CMenuDeskBar::Initialize(THIS)
+{
+    return S_OK;
+}
+
 HRESULT STDMETHODCALLTYPE CMenuDeskBar::GetWindow(HWND *lphwnd)
 {
     if (lphwnd == NULL)
@@ -180,7 +79,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnFocusChangeIS(IUnknown *punkObj, BOOL
     CComPtr<IInputObjectSite> ios;
 
     HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObjectSite, &ios));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     return ios->OnFocusChangeIS(punkObj, fSetFocus);
@@ -228,15 +127,26 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Exec(const GUID *pguidCmdGroup, DWORD nC
 
 HRESULT STDMETHODCALLTYPE CMenuDeskBar::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
 {
-    if (m_Site == NULL)
-        return E_FAIL;
-
     if (IsEqualGUID(guidService, SID_SMenuPopup) ||
         IsEqualGUID(guidService, SID_SMenuBandParent) ||
         IsEqualGUID(guidService, SID_STopLevelBrowser))
     {
         return this->QueryInterface(riid, ppvObject);
     }
+    
+    if (IsEqualGUID(guidService, SID_SMenuBandBottom) ||
+        IsEqualGUID(guidService, SID_SMenuBandBottomSelected) ||
+        IsEqualGUID(guidService, SID_SMenuBandChild))
+    {
+        if (m_Client == NULL)
+            return E_NOINTERFACE;
+
+        return IUnknown_QueryService(m_Client, guidService, riid, ppvObject);
+    }
+
+
+    if (m_Site == NULL)
+        return E_NOINTERFACE;
 
     return IUnknown_QueryService(m_Site, guidService, riid, ppvObject);
 }
@@ -251,7 +161,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::HasFocusIO()
     CComPtr<IInputObject> io;
 
     HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObject, &io));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     return io->HasFocusIO();
@@ -262,7 +172,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::TranslateAcceleratorIO(LPMSG lpMsg)
     CComPtr<IInputObject> io;
 
     HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObject, &io));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     return io->TranslateAcceleratorIO(lpMsg);
@@ -284,15 +194,15 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetClient(IUnknown *punkClient)
     }
 
     hr = punkClient->QueryInterface(IID_PPV_ARG(IUnknown, &m_Client));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &pDeskBandClient));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     hr = pDeskBandClient->SetDeskBarSite(static_cast<IDeskBar*>(this));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     return IUnknown_GetWindow(m_Client, &m_ClientWindow);
@@ -313,13 +223,21 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnPosRectChangeDB(LPRECT prc)
 {
     if (prc == NULL)
         return E_POINTER;
+
     return S_OK;
 }
 
 HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetSite(IUnknown *pUnkSite)
 {
+    // Windows closes the bar if this is called when the bar is shown
+
+    if (m_Shown)
+        _CloseBar();
+
     m_Site = pUnkSite;
 
+    IUnknown_QueryService(m_Site, SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &m_SubMenuParent));
+
     return S_OK;
 }
 
@@ -331,90 +249,6 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::GetSite(REFIID riid, void **ppvSite)
     return m_Site->QueryInterface(riid, ppvSite);
 }
 
-LRESULT CMenuDeskBar::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
-{
-    HRESULT hr;
-
-    if (m_Client)
-    {
-        RECT rc;
-
-        GetClientRect(&rc);
-
-        if (m_Banner != NULL)
-        {
-            BITMAP bm;
-            ::GetObject(m_Banner, sizeof(bm), &bm);
-            rc.left += bm.bmWidth;
-        }
-
-        ::SetWindowPos(m_ClientWindow, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
-    }
-
-    return 0;
-}
-
-LRESULT CMenuDeskBar::OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
-{
-    CComPtr<IWinEventHandler>               winEventHandler;
-    LRESULT                                 result;
-    HRESULT                                 hr;
-
-    result = 0;
-    if (m_Client.p != NULL)
-    {
-        hr = m_Client->QueryInterface(IID_PPV_ARG(IWinEventHandler, &winEventHandler));
-        if (SUCCEEDED(hr) && winEventHandler.p != NULL)
-            hr = winEventHandler->OnWinEvent(NULL, uMsg, wParam, lParam, &result);
-    }
-    return result;
-}
-
-LRESULT CMenuDeskBar::OnWindowPosChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
-{
-    return 0;
-}
-
-LRESULT CMenuDeskBar::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
-{
-    bHandled = FALSE;
-
-    if (m_Banner && !m_IconSize)
-    {
-        BITMAP bm;
-        PAINTSTRUCT ps;
-        HDC hdc = BeginPaint(&ps);
-
-        HDC hdcMem = ::CreateCompatibleDC(hdc);
-        HGDIOBJ hbmOld = ::SelectObject(hdcMem, m_Banner);
-
-        ::GetObject(m_Banner, sizeof(bm), &bm);
-
-        RECT rc;
-        if (!GetClientRect(&rc))
-            WARN("GetClientRect failed\n");
-
-        const int bx = bm.bmWidth;
-        const int by = bm.bmHeight;
-        const int cy = rc.bottom;
-
-        TRACE("Painting banner: %d by %d\n", bm.bmWidth, bm.bmHeight);
-
-        if (!::StretchBlt(hdc, 0, 0, bx, cy - by, hdcMem, 0, 0, bx, 1, SRCCOPY))
-            WARN("StretchBlt failed\n");
-
-        if (!::BitBlt(hdc, 0, cy - by, bx, by, hdcMem, 0, 0, SRCCOPY))
-            WARN("BitBlt failed\n");
-
-        ::SelectObject(hdcMem, hbmOld);
-        ::DeleteDC(hdcMem);
-
-        EndPaint(&ps);
-    }
-
-    return TRUE;
-}
-
 HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP_POPUPFLAGS dwFlags)
 {
     HRESULT hr;
@@ -427,24 +261,25 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
         return E_FAIL;
 
     hr = IUnknown_QueryService(m_Client, SID_SMenuBandChild, IID_PPV_ARG(IOleCommandTarget, &oct));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &dbc));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
+    // Windows calls this, but it appears to be unimplemented?
     hr = dbc->SetModeDBC(1);
     // Allow it to fail with E_NOTIMPL.
 
     // No clue about the arg, using anything != 0
     hr = dbc->UIActivateDBC(TRUE);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     RECT rc = { 0 };
     hr = dbc->GetSize(0, &rc);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     // Unknown meaning
@@ -452,12 +287,11 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
     const int CMD_EXEC_OPT = 0;
 
     hr = IUnknown_QueryServiceExec(m_Client, SID_SMenuBandChild, &CLSID_MenuBand, CMD, CMD_EXEC_OPT, NULL, NULL);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     ::AdjustWindowRect(&rc, ::GetWindowLong(m_hWnd, GWL_STYLE), FALSE);
-    rc.right -= rc.left;
-    rc.bottom -= rc.top;
+    ::OffsetRect(&rc, -rc.left, -rc.top);
 
     if (m_Banner != NULL)
     {
@@ -465,25 +299,72 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
         ::GetObject(m_Banner, sizeof(bm), &bm);
         rc.right += bm.bmWidth;
     }
+    
+    int x, y, cx, cy;
 
-    int x = ppt->x;
-    int y = ppt->y - rc.bottom;
-    int cx = rc.right;
-    int cy = rc.bottom;
+    RECT rcWorkArea;
+    GetWindowRect(GetDesktopWindow(), &rcWorkArea);
+    int waHeight = rcWorkArea.bottom - rcWorkArea.top;
 
-    if (y < 0)
+    switch (dwFlags & 0xFF000000)
     {
-        y = 0;
+    case MPPF_BOTTOM:
+        x = ppt->x;
+        cx = rc.right - rc.left;
+        y = ppt->y - rc.bottom;
+        cy = rc.bottom - rc.top;
+        break;
+    case MPPF_RIGHT:
+        x = ppt->x + rc.left;
+        cx = rc.right - rc.left;
+        y = ppt->y + rc.top;
+        cy = rc.bottom - rc.top;
+        break;
+    case MPPF_TOP | MPPF_ALIGN_LEFT:
+        x = ppt->x - rc.right;
+        cx = rc.right - rc.left;
+        y = ppt->y + rc.top;
+        cy = rc.bottom - rc.top;
+        break;
+    case MPPF_TOP | MPPF_ALIGN_RIGHT:
+        x = ppt->x;
+        cx = rc.right - rc.left;
+        y = ppt->y + rc.top;
+        cy = rc.bottom - rc.top;
+        break;
+    }
+
+    if (x + cx > rcWorkArea.right)
+    {
+        // FIXME: Works, but it's oversimplified.
+        x = prcExclude->left - cx;
+        dwFlags = (dwFlags & (~MPPF_TOP)) | MPPF_LEFT;
+    }
+
+    if (y < rcWorkArea.top)
+    {
+        y = rcWorkArea.top;
+    }
+
+    if (cy > waHeight)
+    {
+        cy = waHeight;
+    }
+    
+    if (y + cy > rcWorkArea.bottom)
+    {
+        y = rcWorkArea.bottom - cy;
     }
 
-    // if (y+cy > work area height) cy = work area height - y
+    this->SetWindowPos(HWND_TOPMOST, x, y, cx, cy, SWP_SHOWWINDOW | SWP_NOACTIVATE);
 
-    this->SetWindowPos(HWND_TOPMOST, x, y, cx, cy, SWP_SHOWWINDOW);
+    m_ShowFlags = dwFlags;
+    m_Shown = true;
 
     // HACK: The bar needs to be notified of the size AFTER it is shown.
     // Quick & dirty way of getting it done.
     BOOL bHandled;
-    OnSize(WM_SIZE, 0, 0, bHandled);
+    _OnSize(WM_SIZE, 0, 0, bHandled);
 
     UIActivateIO(TRUE, NULL);
 
@@ -500,11 +381,11 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetIconSize(THIS_ DWORD iIcon)
     const int CMD_EXEC_OPT = iIcon ? 0 : 2; // seems to work
 
     hr = IUnknown_QueryServiceExec(m_Client, SID_SMenuBandChild, &CLSID_MenuBand, CMD, CMD_EXEC_OPT, NULL, NULL);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     BOOL bHandled;
-    OnSize(WM_SIZE, 0, 0, bHandled);
+    _OnSize(WM_SIZE, 0, 0, bHandled);
 
     return hr;
 }
@@ -521,7 +402,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetBitmap(THIS_ HBITMAP hBitmap)
     m_Banner = hBitmap;
 
     BOOL bHandled;
-    OnSize(WM_SIZE, 0, 0, bHandled);
+    _OnSize(WM_SIZE, 0, 0, bHandled);
 
     return S_OK;
 }
@@ -533,81 +414,249 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::GetBitmap(THIS_ HBITMAP* phBitmap)
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnSelect(
-    DWORD dwSelectType)
+HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetSubMenu(IMenuPopup *pmp, BOOL fSet)
 {
-    CComPtr<IDeskBarClient> dbc;
-    HRESULT hr;
-    
-    bool bubbleUp = false;
-    bool cancel = false;
+    // Called by the MenuBand to assign itself as the logical child of the DeskBar
+
+    if (fSet)
+    {
+        m_SubMenuChild = pmp;
+    }
+    else
+    {
+        if (m_SubMenuChild)
+        {
+            if (pmp == m_SubMenuChild)
+            {
+                m_SubMenuChild = NULL;
+            }
+        }
+    }
+    return S_OK;
+}
+
+HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnSelect(DWORD dwSelectType)
+{
+    /* As far as I can tell, the submenu hierarchy looks like this:
+
+    The DeskBar's Child is the Band it contains.
+    The DeskBar's Parent is the SID_SMenuPopup of the Site.
+
+    The Band's Child is the IMenuPopup of the child submenu.
+    The Band's Parent is the SID_SMenuPopup of the Site (the DeskBar).
+
+    When the DeskBar receives a selection event:
+        If it requires closing the window, it will notify the Child (Band) using CancelLevel.
+        If it has to spread upwards (everything but CancelLevel), it will notify the Parent.
+
+    When the Band receives a selection event, this is where it gets fuzzy:
+        In which cases does it call the Parent? Probably not CancelLevel.
+        In which cases does it call the Child?
+        How does it react to calls?
+
+    */
 
     switch (dwSelectType)
     {
-    case MPOS_FULLCANCEL:
     case MPOS_EXECUTE:
-        bubbleUp = true;
-        cancel = true;
-        break;
+    case MPOS_FULLCANCEL:
     case MPOS_CANCELLEVEL:
-        cancel = true;
-        break;
+
+        _CloseBar();
+
+        if (dwSelectType == MPOS_CANCELLEVEL)
+            return S_OK;
+
     case MPOS_SELECTLEFT:
     case MPOS_SELECTRIGHT:
-        // if unhandled, spread upwards?
-        bubbleUp = true;
-        break;
     case MPOS_CHILDTRACKING:
+        if (m_SubMenuParent)
+            return m_SubMenuParent->OnSelect(dwSelectType);
         break;
     }
 
-    if (cancel)
+    return S_OK;
+}
+
+HRESULT CMenuDeskBar::_CloseBar()
+{
+    CComPtr<IDeskBarClient> dbc;
+    HRESULT hr;
+
+    m_Shown = false;
+
+    if (m_SubMenuChild)
     {
-        hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &dbc));
-        if (FAILED(hr))
+        hr = m_SubMenuChild->OnSelect(MPOS_CANCELLEVEL);
+        if (FAILED_UNEXPECTEDLY(hr))
             return hr;
+    }
+
+    hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &dbc));
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
 
-        hr = dbc->UIActivateDBC(FALSE);
+    hr = dbc->UIActivateDBC(FALSE);
+    if (FAILED_UNEXPECTEDLY(hr))
+        return hr;
+
+    SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE);
+
+    return UIActivateIO(FALSE, NULL);
+}
+
+BOOL CMenuDeskBar::_IsSubMenuParent(HWND hwnd)
+{
+    CComPtr<IMenuPopup> popup = m_SubMenuParent;
+
+    while (popup)
+    {
+        HRESULT hr;
+        CComPtr<IOleWindow> window;
+
+        hr = popup->QueryInterface(IID_PPV_ARG(IOleWindow, &window));
+        if (FAILED_UNEXPECTEDLY(hr))
+            return FALSE;
+
+        HWND parent;
+
+        hr = window->GetWindow(&parent);
+        if (SUCCEEDED(hr) && hwnd == parent)
+            return TRUE;
+
+        popup = NULL;
+        hr = IUnknown_GetSite(window, IID_PPV_ARG(IMenuPopup, &popup));
         if (FAILED(hr))
-            return hr;
+            return FALSE;
+    }
+
+    return FALSE;
+}
+
+LRESULT CMenuDeskBar::_OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+{
+    if (m_Client)
+    {
+        RECT rc;
+
+        GetClientRect(&rc);
+
+        if (m_Banner != NULL)
+        {
+            BITMAP bm;
+            ::GetObject(m_Banner, sizeof(bm), &bm);
+            rc.left += bm.bmWidth;
+        }
+
+        ::SetWindowPos(m_ClientWindow, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, 0);
+    }
+
+    return 0;
+}
+
+LRESULT CMenuDeskBar::_OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+{
+    if (!m_Client)
+        return 0;
 
-        SetWindowPos(m_hWnd, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
+    CComPtr<IWinEventHandler> winEventHandler;
+    HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IWinEventHandler, &winEventHandler));
+    if (FAILED_UNEXPECTEDLY(hr))
+        return 0;
 
-        UIActivateIO(FALSE, NULL);
+    if (winEventHandler)
+    {
+        LRESULT result;
+        hr = winEventHandler->OnWinEvent(NULL, uMsg, wParam, lParam, &result);
+        if (FAILED_UNEXPECTEDLY(hr))
+            return 0;
+        return result;
     }
 
-    if (bubbleUp && m_SubMenuParent)
+    return 0;
+}
+
+LRESULT CMenuDeskBar::_OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+{
+    bHandled = FALSE;
+
+    if (m_Banner && !m_IconSize)
     {
-        hr = m_SubMenuParent->OnSelect(MPOS_CANCELLEVEL); // why??
-        return hr;
+        BITMAP bm;
+        PAINTSTRUCT ps;
+        HDC hdc = BeginPaint(&ps);
+
+        HDC hdcMem = ::CreateCompatibleDC(hdc);
+        HGDIOBJ hbmOld = ::SelectObject(hdcMem, m_Banner);
+
+        ::GetObject(m_Banner, sizeof(bm), &bm);
+
+        RECT rc;
+        if (!GetClientRect(&rc))
+            WARN("GetClientRect failed\n");
+
+        const int bx = bm.bmWidth;
+        const int by = bm.bmHeight;
+        const int cy = rc.bottom;
+
+        TRACE("Painting banner: %d by %d\n", bm.bmWidth, bm.bmHeight);
+
+        if (!::StretchBlt(hdc, 0, 0, bx, cy - by, hdcMem, 0, 0, bx, 1, SRCCOPY))
+            WARN("StretchBlt failed\n");
+
+        if (!::BitBlt(hdc, 0, cy - by, bx, by, hdcMem, 0, 0, SRCCOPY))
+            WARN("BitBlt failed\n");
+
+        ::SelectObject(hdcMem, hbmOld);
+        ::DeleteDC(hdcMem);
+
+        EndPaint(&ps);
     }
 
-    return S_OK;
+    return TRUE;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetSubMenu(
-    IMenuPopup *pmp,
-    BOOL fSet)
+LRESULT CMenuDeskBar::_OnActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
 {
-    if (fSet)
+    // BUG in ReactOS: WM_ACTIVATE/WA_INACTIVE makes no sense with lParam==hWnd
+    if (LOWORD(wParam) != 0 || reinterpret_cast<HWND>(lParam) == m_hWnd)
     {
-        m_SubMenuParent = pmp;
+        return 0;
     }
-    else
+
+    // HACK! I just want it to work !!!
+    CComPtr<IDeskBar> db;
+    HRESULT hr = IUnknown_QueryService(m_Client, SID_SMenuBandChild, IID_PPV_ARG(IDeskBar, &db));
+    if (FAILED_UNEXPECTEDLY(hr))
+        return 0;
+
+    CComPtr<IUnknown> punk;
+
+    hr = db->GetClient(&punk);
+    if (FAILED_UNEXPECTEDLY(hr))
+        return 0;
+
+    if (!punk && m_Shown)
     {
-        if (m_SubMenuParent)
+        if (!_IsSubMenuParent(reinterpret_cast<HWND>(lParam)))
         {
-            if (SHIsSameObject(pmp, m_SubMenuParent))
-            {
-                m_SubMenuParent = NULL;
-            }
+            OnSelect(MPOS_FULLCANCEL);
         }
     }
-    return S_OK;
+
+    return 0;
 }
 
+LRESULT CMenuDeskBar::_OnMouseActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+{
+    return MA_NOACTIVATE;
+}
 
-HRESULT STDMETHODCALLTYPE CMenuDeskBar::Initialize(THIS)
+LRESULT CMenuDeskBar::_OnAppActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
 {
-    return S_OK;
+    if (wParam == 0 && m_Shown)
+    {
+        OnSelect(MPOS_FULLCANCEL);
+    }
+    return 0;
 }