[RSHELL]
[reactos.git] / base / shell / rshell / CMenuBand.cpp
index 49c3c67..e08e769 100644 (file)
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
 #include "precomp.h"
-#include "wraplog.h"
+#include <windowsx.h>
+#include <shlwapi_undoc.h>
 
 WINE_DEFAULT_DEBUG_CHANNEL(CMenuBand);
 
-#define WRAP_LOG 0
+#define TBSTYLE_EX_VERTICAL 4
+
+#define TIMERID_HOTTRACK 1
+#define SUBCLASS_ID_MENUBAND 1
 
 extern "C" BOOL WINAPI Shell_GetImageLists(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList);
 
@@ -39,15 +43,32 @@ public:
     HRESULT ShowWindow(BOOL fShow);
     HRESULT Close();
 
+    BOOL IsWindowOwner(HWND hwnd) { return m_hwnd && m_hwnd == hwnd; }
+
     virtual HRESULT FillToolbar() = 0;
+    virtual HRESULT PopupItem(UINT uItem) = 0;
+    virtual HRESULT HasSubMenu(UINT uItem) = 0;
+    virtual HRESULT OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult);
+    virtual HRESULT OnContextMenu(NMMOUSE * rclick) = 0;
+
+    HRESULT OnHotItemChange(const NMTBHOTITEM * hot);
+
+    HRESULT PopupSubMenu(UINT index, IShellMenu* childShellMenu);
+    HRESULT PopupSubMenu(UINT index, HMENU menu);
+    HRESULT DoContextMenu(IContextMenu* contextMenu);
 
+    static LRESULT CALLBACK s_SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 protected:
 
     static const UINT WM_USER_SHOWPOPUPMENU = WM_USER + 1;
 
+    LRESULT CALLBACK SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
+
     CMenuBand *m_menuBand;
     HWND m_hwnd;
     DWORD m_dwMenuFlags;
+    INT m_hotItem;
+    WNDPROC m_SubclassOld;
 };
 
 class CMenuStaticToolbar : public CMenuToolbarBase
@@ -60,6 +81,10 @@ public:
     HRESULT GetMenu(HMENU *phmenu, HWND *phwnd, DWORD *pdwFlags);
 
     virtual HRESULT FillToolbar();
+    virtual HRESULT PopupItem(UINT uItem);
+    virtual HRESULT HasSubMenu(UINT uItem);
+    virtual HRESULT OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult);
+    virtual HRESULT OnContextMenu(NMMOUSE * rclick);
 
 private:
     HMENU m_hmenu;
@@ -75,8 +100,13 @@ public:
     HRESULT GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REFIID riid, void **ppv);
 
     virtual HRESULT FillToolbar();
+    virtual HRESULT PopupItem(UINT uItem);
+    virtual HRESULT HasSubMenu(UINT uItem);
+    virtual HRESULT OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult);
+    virtual HRESULT OnContextMenu(NMMOUSE * rclick);
 
 private:
+    LPITEMIDLIST GetPidlFromId(UINT uItem, INT* pIndex);
 
     IShellFolder * m_shellFolder;
     LPCITEMIDLIST m_idList;
@@ -103,25 +133,6 @@ public:
     ~CMenuBand();
 
 private:
-#if WRAP_LOG
-    IUnknown          * m_IUnknown;
-    IDeskBand         * m_IDeskBand;
-    IDockingWindow    * m_IDockingWindow;
-    IOleWindow        * m_IOleWindow;
-    IObjectWithSite   * m_IObjectWithSite;
-    IInputObject      * m_IInputObject;
-    IPersistStream    * m_IPersistStream;
-    IPersist          * m_IPersist;
-    IOleCommandTarget * m_IOleCommandTarget;
-    IServiceProvider  * m_IServiceProvider;
-    IMenuPopup        * m_IMenuPopup;
-    IDeskBar          * m_IDeskBar;
-    IMenuBand         * m_IMenuBand;
-    IShellMenu2       * m_IShellMenu2;
-    IShellMenu        * m_IShellMenu;
-    IWinEventHandler  * m_IWinEventHandler;
-    IShellMenuAcc     * m_IShellMenuAcc;
-#else
     IOleWindow *m_site;
     IShellMenuCallback *m_psmc;
 
@@ -133,10 +144,12 @@ private:
     DWORD m_dwFlags;
     PVOID m_UserData;
     HMENU m_hmenu;
-#endif
+    HWND m_menuOwner;
 
     BOOL m_useBigIcons;
 
+    HWND m_topLevelWindow;
+
 public:
 
     // *** IDeskBand methods ***
@@ -222,6 +235,8 @@ public:
     virtual HRESULT STDMETHODCALLTYPE IsEmpty(THIS);
 
     HRESULT CallCBWithId(UINT Id, UINT uMsg, WPARAM wParam, LPARAM lParam);
+    HRESULT CallCBWithPidl(LPITEMIDLIST pidl, UINT uMsg, WPARAM wParam, LPARAM lParam);
+    HRESULT TrackPopup(HMENU popup, INT x, INT y);
 
     BOOL UseBigIcons() {
         return m_useBigIcons;
@@ -250,7 +265,7 @@ public:
     END_COM_MAP()
 
 private:
-    HRESULT _CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam);
+    HRESULT _CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam, UINT id = 0, LPITEMIDLIST pidl = NULL);
 };
 
 extern "C"
@@ -271,621 +286,277 @@ HRESULT CMenuBand_Constructor(REFIID riid, LPVOID *ppv)
     return hr;
 }
 
-
-#if WRAP_LOG
-CMenuBand::CMenuBand()
-{
-    HRESULT hr;
-    WrapLogOpen();
-
-    hr = CoCreateInstance(CLSID_MenuBand, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IShellMenu, &m_IShellMenu));
-    hr = m_IShellMenu->QueryInterface(IID_PPV_ARG(IUnknown, &m_IUnknown));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IDeskBand, &m_IDeskBand));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IDockingWindow, &m_IDockingWindow));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IOleWindow, &m_IOleWindow));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IObjectWithSite, &m_IObjectWithSite));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IInputObject, &m_IInputObject));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IPersistStream, &m_IPersistStream));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IPersist, &m_IPersist));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IOleCommandTarget, &m_IOleCommandTarget));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IServiceProvider, &m_IServiceProvider));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IMenuPopup, &m_IMenuPopup));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IDeskBar, &m_IDeskBar));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IMenuBand, &m_IMenuBand));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IShellMenu2, &m_IShellMenu2));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IWinEventHandler, &m_IWinEventHandler));
-    hr = m_IUnknown->QueryInterface(IID_PPV_ARG(IShellMenuAcc, &m_IShellMenuAcc));
-}
-
-CMenuBand::~CMenuBand()
-{
-    m_IUnknown->Release();
-    m_IDeskBand->Release();
-    m_IDockingWindow->Release();
-    m_IOleWindow->Release();
-    m_IObjectWithSite->Release();
-    m_IInputObject->Release();
-    m_IPersistStream->Release();
-    m_IPersist->Release();
-    m_IOleCommandTarget->Release();
-    m_IServiceProvider->Release();
-    m_IMenuPopup->Release();
-    m_IDeskBar->Release();
-    m_IMenuBand->Release();
-    m_IShellMenu2->Release();
-    m_IShellMenu->Release();
-    m_IWinEventHandler->Release();
-    m_IShellMenuAcc->Release();
-    WrapLogClose();
-}
-
-
-// *** IShellMenu2 methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::GetSubMenu(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::GetSubMenu()\n", this);
-    HRESULT hr = m_IShellMenu2->GetSubMenu();
-    WrapLogExit("CMenuBand::GetSubMenu() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::SetToolbar(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::SetToolbar()\n", this);
-    HRESULT hr = m_IShellMenu2->SetToolbar();
-    WrapLogExit("CMenuBand::SetToolbar() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::SetMinWidth(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::SetMinWidth()\n", this);
-    HRESULT hr = m_IShellMenu2->SetMinWidth();
-    WrapLogExit("CMenuBand::SetMinWidth() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::SetNoBorder(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::SetNoBorder()\n", this);
-    HRESULT hr = m_IShellMenu2->SetNoBorder();
-    WrapLogExit("CMenuBand::SetNoBorder() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::SetTheme(THIS)
+CMenuToolbarBase::CMenuToolbarBase(CMenuBand *menuBand) :
+    m_menuBand(menuBand),
+    m_hwnd(NULL),
+    m_dwMenuFlags(0)
 {
-    WrapLogEnter("CMenuBand<%p>::SetTheme()\n", this);
-    HRESULT hr = m_IShellMenu2->SetTheme();
-    WrapLogExit("CMenuBand::SetTheme() = %08x\n", hr);
-    return hr;
 }
 
-
-// *** IShellMenuAcc methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::GetTop(THIS)
+HRESULT CMenuToolbarBase::ShowWindow(BOOL fShow)
 {
-    WrapLogEnter("CMenuBand<%p>::GetTop()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetTop();
-    WrapLogExit("CMenuBand::GetTop() = %08x\n", hr);
-    return hr;
-}
+    ::ShowWindow(m_hwnd, fShow ? SW_SHOW : SW_HIDE);
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetBottom(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::GetBottom()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetBottom();
-    WrapLogExit("CMenuBand::GetBottom() = %08x\n", hr);
-    return hr;
-}
+    HIMAGELIST ilBig, ilSmall;
+    Shell_GetImageLists(&ilBig, &ilSmall);
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetTracked(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::GetBottom()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetBottom();
-    WrapLogExit("CMenuBand::GetBottom() = %08x\n", hr);
-    return hr;
-}
+    if (m_menuBand->UseBigIcons())
+    {
+        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(ilBig));
+    }
+    else
+    {
+        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(ilSmall));
+    }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetParentSite(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::GetBottom()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetBottom();
-    WrapLogExit("CMenuBand::GetBottom() = %08x\n", hr);
-    return hr;
+    return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetState(THIS)
+HRESULT CMenuToolbarBase::Close()
 {
-    WrapLogEnter("CMenuBand<%p>::GetBottom()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetBottom();
-    WrapLogExit("CMenuBand::GetBottom() = %08x\n", hr);
-    return hr;
+    DestroyWindow(m_hwnd);
+    m_hwnd = NULL;
+    return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::DoDefaultAction(THIS)
+HRESULT CMenuToolbarBase::CreateToolbar(HWND hwndParent, DWORD dwFlags)
 {
-    WrapLogEnter("CMenuBand<%p>::GetBottom()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetBottom();
-    WrapLogExit("CMenuBand::GetBottom() = %08x\n", hr);
-    return hr;
-}
+    LONG tbStyles = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
+        TBSTYLE_TOOLTIPS | TBSTYLE_TRANSPARENT | TBSTYLE_REGISTERDROP | TBSTYLE_LIST | TBSTYLE_FLAT | TBSTYLE_CUSTOMERASE |
+        CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NORESIZE | CCS_TOP;
+    LONG tbExStyles = TBSTYLE_EX_DOUBLEBUFFER;
 
-HRESULT STDMETHODCALLTYPE CMenuBand::IsEmpty(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::GetBottom()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetBottom();
-    WrapLogExit("CMenuBand::GetBottom() = %08x\n", hr);
-    return hr;
-}
+    if (dwFlags & SMINIT_VERTICAL)
+    {
+        tbStyles |= CCS_VERT;
+        tbExStyles |= TBSTYLE_EX_VERTICAL | WS_EX_TOOLWINDOW;
+    }
 
-// *** IDeskBand methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::GetBandInfo(DWORD dwBandID, DWORD dwViewMode, DESKBANDINFO *pdbi)
-{
-    WrapLogEnter("CMenuBand<%p>::GetBandInfo(DWORD dwBandID=%d, DWORD dwViewMode=%d, DESKBANDINFO *pdbi=%p)\n", this, dwBandID, dwViewMode, pdbi);
-    HRESULT hr = m_IDeskBand->GetBandInfo(dwBandID, dwViewMode, pdbi);
-    WrapLogExit("CMenuBand::GetBandInfo() = %08x\n", hr);
-    return hr;
-}
+    RECT rc;
 
-// *** IDockingWindow methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::ShowDW(BOOL fShow)
-{
-    WrapLogEnter("CMenuBand<%p>::ShowDW(BOOL fShow=%d)\n", this, fShow);
-    HRESULT hr = m_IDockingWindow->ShowDW(fShow);
-    WrapLogExit("CMenuBand::ShowDW() = %08x\n", hr);
-    return hr;
-}
+    if (!::GetClientRect(hwndParent, &rc) || (rc.left == rc.right) || (rc.top == rc.bottom))
+    {
+        rc.left = 0;
+        rc.top = 0;
+        rc.right = 1;
+        rc.bottom = 1;
+    }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::CloseDW(DWORD dwReserved)
-{
-    WrapLogEnter("CMenuBand<%p>::CloseDW(DWORD dwReserved=%d)\n", this, dwReserved);
-    HRESULT hr = m_IDockingWindow->CloseDW(dwReserved);
-    WrapLogExit("CMenuBand::CloseDW() = %08x\n", hr);
-    return hr;
-}
+    HWND hwndToolbar = CreateWindowEx(
+        tbExStyles, TOOLBARCLASSNAMEW, NULL,
+        tbStyles, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
+        hwndParent, NULL, _AtlBaseModule.GetModuleInstance(), 0);
 
-HRESULT STDMETHODCALLTYPE CMenuBand::ResizeBorderDW(LPCRECT prcBorder, IUnknown *punkToolbarSite, BOOL fReserved)
-{
-    WrapLogEnter("CMenuBand<%p>::ResizeBorderDW(LPCRECT prcBorder=%p, IUnknown *punkToolbarSite=%p, BOOL fReserved=%d)\n", this, prcBorder, punkToolbarSite, fReserved);
-    if (prcBorder) WrapLogMsg("*prcBorder=%s\n", Wrap(*prcBorder));
-    HRESULT hr = m_IDockingWindow->ResizeBorderDW(prcBorder, punkToolbarSite, fReserved);
-    if (prcBorder) WrapLogMsg("*prcBorder=%s\n", Wrap(*prcBorder));
-    WrapLogExit("CMenuBand::ResizeBorderDW() = %08x\n", hr);
-    return hr;
-}
+    if (hwndToolbar == NULL)
+        return E_FAIL;
 
-// *** IOleWindow methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::GetWindow(HWND *phwnd)
-{
-    WrapLogEnter("CMenuBand<%p>::GetWindow(HWND *phwnd=%p)\n", this, phwnd);
-    HRESULT hr = m_IOleWindow->GetWindow(phwnd);
-    if (phwnd) WrapLogMsg("*phwnd=%p\n", *phwnd);
-    WrapLogExit("CMenuBand::GetWindow() = %08x\n", hr);
-    return hr;
-}
-HRESULT STDMETHODCALLTYPE CMenuBand::ContextSensitiveHelp(BOOL fEnterMode)
-{
-    WrapLogEnter("CMenuBand<%p>::ContextSensitiveHelp(BOOL fEnterMode=%d)\n", this, fEnterMode);
-    HRESULT hr = m_IOleWindow->ContextSensitiveHelp(fEnterMode);
-    WrapLogExit("CMenuBand::ContextSensitiveHelp() = %08x\n", hr);
-    return hr;
-}
+    ::SetParent(hwndToolbar, hwndParent);
 
-// *** IWinEventHandler methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *theResult)
-{
-    //WrapLogEnter("CMenuBand<%p>::OnWinEvent(HWND hWnd=%p, UINT uMsg=%u, WPARAM wParam=%08x, LPARAM lParam=%08x, LRESULT *theResult=%p)\n", this, hWnd, uMsg, wParam, lParam, theResult);
-    HRESULT hr = m_IWinEventHandler->OnWinEvent(hWnd, uMsg, wParam, lParam, theResult);
-    //WrapLogExit("CMenuBand::OnWinEvent() = %08x\n", hr);
-    return hr;
-}
+    m_hwnd = hwndToolbar;
 
-HRESULT STDMETHODCALLTYPE CMenuBand::IsWindowOwner(HWND hWnd)
-{
-    //WrapLogEnter("CMenuBand<%p>::IsWindowOwner(HWND hWnd=%08x)\n", this, hWnd);
-    HRESULT hr = m_IWinEventHandler->IsWindowOwner(hWnd);
-    //WrapLogExit("CMenuBand::IsWindowOwner() = %08x\n", hr);
-    return hr;
-}
+    /* Identify the version of the used Common Controls DLL by sending the size of the TBBUTTON structure */
+    SendMessageW(m_hwnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
 
-// *** IObjectWithSite methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::SetSite(IUnknown *pUnkSite)
-{
-    WrapLogEnter("CMenuBand<%p>::SetSite(IUnknown *pUnkSite=%p)\n", this, pUnkSite);
-    HRESULT hr = m_IObjectWithSite->SetSite(pUnkSite);
-    WrapLogExit("CMenuBand::SetSite() = %08x\n", hr);
-    return hr;
-}
+    HIMAGELIST ilBig, ilSmall;
+    Shell_GetImageLists(&ilBig, &ilSmall);
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetSite(REFIID riid, PVOID *ppvSite)
-{
-    WrapLogEnter("CMenuBand<%p>::GetSite(REFIID riid=%s, PVOID *ppvSite=%p)\n", this, Wrap(riid), ppvSite);
-    HRESULT hr = m_IObjectWithSite->GetSite(riid, ppvSite);
-    if (ppvSite) WrapLogMsg("*ppvSite=%p\n", *ppvSite);
-    WrapLogExit("CMenuBand::GetSite() = %08x\n", hr);
-    return hr;
-}
+    //if (dwFlags & SMINIT_TOPLEVEL)
+    //{
+    //    /* Hide the placeholders for the button images */
+    //    SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, 0);
+    //}
+    //else
+    if (m_menuBand->UseBigIcons())
+    {
+        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(ilBig));
+    }
+    else
+    {
+        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(ilSmall));
+    }
 
-// *** IInputObject methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::UIActivateIO(BOOL fActivate, LPMSG lpMsg)
-{
-    WrapLogEnter("CMenuBand<%p>::UIActivateIO(BOOL fActivate=%d, LPMSG lpMsg=%p)\n", this, fActivate, lpMsg);
-    HRESULT hr = m_IInputObject->UIActivateIO(fActivate, lpMsg);
-    WrapLogExit("CMenuBand::UIActivateIO() = %08x\n", hr);
-    return hr;
-}
+    SetWindowLongPtr(m_hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
+    m_SubclassOld = (WNDPROC) SetWindowLongPtr(m_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(CMenuToolbarBase::s_SubclassProc));
 
-HRESULT STDMETHODCALLTYPE CMenuBand::HasFocusIO()
-{
-    WrapLogEnter("CMenuBand<%p>::HasFocusIO()\n", this);
-    HRESULT hr = m_IInputObject->HasFocusIO();
-    WrapLogExit("CMenuBand::HasFocusIO() = %08x\n", hr);
-    return hr;
+    return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::TranslateAcceleratorIO(LPMSG lpMsg)
+HRESULT CMenuToolbarBase::GetWindow(HWND *phwnd)
 {
-    WrapLogEnter("CMenuBand<%p>::TranslateAcceleratorIO(LPMSG lpMsg=%p)\n", this, lpMsg);
-    if (lpMsg) WrapLogMsg("*lpMsg=%s\n", Wrap(*lpMsg));
-    HRESULT hr = m_IInputObject->TranslateAcceleratorIO(lpMsg);
-    WrapLogExit("CMenuBand::TranslateAcceleratorIO() = %08x\n", hr);
-    return hr;
-}
+    if (!phwnd)
+        return E_FAIL;
 
-// *** IPersistStream methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::IsDirty()
-{
-    WrapLogEnter("CMenuBand<%p>::IsDirty()\n", this);
-    HRESULT hr = m_IPersistStream->IsDirty();
-    WrapLogExit("CMenuBand::IsDirty() = %08x\n", hr);
-    return hr;
-}
-HRESULT STDMETHODCALLTYPE CMenuBand::Load(IStream *pStm)
-{
-    WrapLogEnter("CMenuBand<%p>::Load(IStream *pStm=%p)\n", this, pStm);
-    HRESULT hr = m_IPersistStream->Load(pStm);
-    WrapLogExit("CMenuBand::Load() = %08x\n", hr);
-    return hr;
-}
-HRESULT STDMETHODCALLTYPE CMenuBand::Save(IStream *pStm, BOOL fClearDirty)
-{
-    WrapLogEnter("CMenuBand<%p>::Save(IStream *pStm=%p, BOOL fClearDirty=%d)\n", this, pStm, fClearDirty);
-    HRESULT hr = m_IPersistStream->Save(pStm, fClearDirty);
-    WrapLogExit("CMenuBand::Save() = %08x\n", hr);
-    return hr;
-}
-HRESULT STDMETHODCALLTYPE CMenuBand::GetSizeMax(ULARGE_INTEGER *pcbSize)
-{
-    WrapLogEnter("CMenuBand<%p>::GetSizeMax(ULARGE_INTEGER *pcbSize=%p)\n", this, pcbSize);
-    HRESULT hr = m_IPersistStream->GetSizeMax(pcbSize);
-    WrapLogExit("CMenuBand::GetSizeMax() = %08x\n", hr);
-    return hr;
-}
+    *phwnd = m_hwnd;
 
-// *** IPersist methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::GetClassID(CLSID *pClassID)
-{
-    WrapLogEnter("CMenuBand<%p>::GetClassID(CLSID *pClassID=%p)\n", this, pClassID);
-    HRESULT hr = m_IPersist->GetClassID(pClassID);
-    if (pClassID) WrapLogMsg("*pClassID=%s\n", Wrap(*pClassID));
-    WrapLogExit("CMenuBand::GetClassID() = %08x\n", hr);
-    return hr;
+    return S_OK;
 }
 
-// *** IOleCommandTarget methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::QueryStatus(const GUID *pguidCmdGroup, ULONG cCmds, OLECMD prgCmds [], OLECMDTEXT *pCmdText)
+LRESULT CALLBACK CMenuToolbarBase::s_SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
-    WrapLogEnter("CMenuBand<%p>::QueryStatus(const GUID *pguidCmdGroup=%p, ULONG cCmds=%u, prgCmds=%p, pCmdText=%p)\n", this, pguidCmdGroup, cCmds, prgCmds, pCmdText);
-    HRESULT hr = m_IOleCommandTarget->QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText);
-    if (pguidCmdGroup) WrapLogMsg("*pguidCmdGroup=%s\n", Wrap(*pguidCmdGroup));
-    WrapLogExit("CMenuBand::QueryStatus() = %08x\n", hr);
-    return hr;
+    CMenuToolbarBase * pthis = reinterpret_cast<CMenuToolbarBase *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
+    return pthis->SubclassProc(hWnd, uMsg, wParam, lParam);
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::Exec(const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
+LRESULT CMenuToolbarBase::SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
-    bool b;
-
-    WrapLogEnter("CMenuBand<%p>::Exec(const GUID *pguidCmdGroup=%p, DWORD nCmdID=%d, DWORD nCmdexecopt=%d, VARIANT *pvaIn=%p, VARIANT *pvaOut=%p)\n", this, pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
-
-    if (pguidCmdGroup && IsEqualGUID(*pguidCmdGroup, CLSID_MenuBand))
+    switch (uMsg)
     {
-        if (nCmdID == 19) // popup
+    case WM_TIMER:
+        if (wParam == TIMERID_HOTTRACK)
         {
-            b = true;
+            PopupItem(m_hotItem);
+            KillTimer(hWnd, TIMERID_HOTTRACK);
         }
     }
 
-
-    if (pguidCmdGroup) WrapLogMsg("*pguidCmdGroup=%s\n", Wrap(*pguidCmdGroup));
-    HRESULT hr = m_IOleCommandTarget->Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
-    WrapLogExit("CMenuBand::Exec() = %08x\n", hr);
-    return hr;
+    return m_SubclassOld(hWnd, uMsg, wParam, lParam);
 }
 
-// *** IServiceProvider methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
+HRESULT CMenuToolbarBase::OnHotItemChange(const NMTBHOTITEM * hot)
 {
-    WrapLogEnter("CMenuBand<%p>::QueryService(REFGUID guidService=%s, REFIID riid=%s, void **ppvObject=%p)\n", this, Wrap(guidService), Wrap(riid), ppvObject);
-
-    if (IsEqualIID(guidService, SID_SMenuBandChild))
+    if (hot->dwFlags & HICF_LEAVING)
     {
-        WrapLogMsg("SID is SID_SMenuBandChild. Using QueryInterface of self instead of wrapped object.\n");
-        HRESULT hr = this->QueryInterface(riid, ppvObject);
-        if (ppvObject) WrapLogMsg("*ppvObject=%p\n", *ppvObject);
-        WrapLogExit("CMenuBand::QueryService() = %08x\n", hr);
-        return hr;
+        KillTimer(m_hwnd, TIMERID_HOTTRACK);
     }
-    else
+    else if (m_hotItem != hot->idNew)
     {
-        WrapLogMsg("SID not identified.\n");
-    }
-    HRESULT hr = m_IServiceProvider->QueryService(guidService, riid, ppvObject);
-    if (ppvObject) WrapLogMsg("*ppvObject=%p\n", *ppvObject);
-    WrapLogExit("CMenuBand::QueryService() = %08x\n", hr);
-    return hr;
-}
-
-
-// *** IMenuPopup methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::Popup(POINTL *ppt, RECTL *prcExclude, MP_POPUPFLAGS dwFlags)
-{
-    WrapLogEnter("CMenuBand<%p>::Popup(POINTL *ppt=%p, RECTL *prcExclude=%p, MP_POPUPFLAGS dwFlags=%08x)\n", this, ppt, prcExclude, dwFlags);
-    HRESULT hr = m_IMenuPopup->Popup(ppt, prcExclude, dwFlags);
-    WrapLogExit("CMenuBand::Popup() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::OnSelect(DWORD dwSelectType)
-{
-    WrapLogEnter("CMenuBand<%p>::OnSelect(DWORD dwSelectType=%08x)\n", this, dwSelectType);
-    HRESULT hr = m_IMenuPopup->OnSelect(dwSelectType);
-    WrapLogExit("CMenuBand::OnSelect() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::SetSubMenu(IMenuPopup *pmp, BOOL fSet)
-{
-    WrapLogEnter("CMenuBand<%p>::SetSubMenu(IMenuPopup *pmp=%p, BOOL fSet=%d)\n", this, pmp, fSet);
-    HRESULT hr = m_IMenuPopup->SetSubMenu(pmp, fSet);
-    WrapLogExit("CMenuBand::SetSubMenu() = %08x\n", hr);
-    return hr;
-}
-
-
-// *** IDeskBar methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::SetClient(IUnknown *punkClient)
-{
-    WrapLogEnter("CMenuBand<%p>::SetClient(IUnknown *punkClient=%p)\n", this, punkClient);
-    HRESULT hr = m_IDeskBar->SetClient(punkClient);
-    WrapLogExit("CMenuBand::SetClient() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::GetClient(IUnknown **ppunkClient)
-{
-    WrapLogEnter("CMenuBand<%p>::GetClient(IUnknown **ppunkClient=%p)\n", this, ppunkClient);
-    HRESULT hr = m_IDeskBar->GetClient(ppunkClient);
-    if (ppunkClient) WrapLogMsg("*ppunkClient=%p\n", *ppunkClient);
-    WrapLogExit("CMenuBand::GetClient() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::OnPosRectChangeDB(RECT *prc)
-{
-    WrapLogEnter("CMenuBand<%p>::OnPosRectChangeDB(RECT *prc=%p)\n", this, prc);
-    HRESULT hr = m_IDeskBar->OnPosRectChangeDB(prc);
-    if (prc) WrapLogMsg("*prc=%s\n", Wrap(*prc));
-    WrapLogExit("CMenuBand::OnPosRectChangeDB() = %08x\n", hr);
-    return hr;
-}
+        if (HasSubMenu(hot->idNew) == S_OK)
+        {
+            DWORD elapsed = 0;
+            SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &elapsed, 0);
 
+            m_hotItem = hot->idNew;
 
-// *** IMenuBand methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::IsMenuMessage(MSG *pmsg)
-{
-    //WrapLogEnter("CMenuBand<%p>::IsMenuMessage(MSG *pmsg=%p)\n", this, pmsg);
-    HRESULT hr = m_IMenuBand->IsMenuMessage(pmsg);
-    //WrapLogExit("CMenuBand::IsMenuMessage() = %08x\n", hr);
-    return hr;
-}
+            SetTimer(m_hwnd, TIMERID_HOTTRACK, elapsed, NULL);
+        }
+    }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::TranslateMenuMessage(MSG *pmsg, LRESULT *plRet)
-{
-    //WrapLogEnter("CMenuBand<%p>::TranslateMenuMessage(MSG *pmsg=%p, LRESULT *plRet=%p)\n", this, pmsg, plRet);
-    HRESULT hr = m_IMenuBand->TranslateMenuMessage(pmsg, plRet);
-    //WrapLogExit("CMenuBand::TranslateMenuMessage(*plRet=%d) = %08x\n", *plRet, hr);
-    return hr;
+    m_menuBand->OnSelect(MPOS_CHILDTRACKING);
+    return S_OK;
 }
 
-// *** IShellMenu methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::Initialize(IShellMenuCallback *psmc, UINT uId, UINT uIdAncestor, DWORD dwFlags)
+HRESULT CMenuToolbarBase::PopupSubMenu(UINT index, IShellMenu* childShellMenu)
 {
-    WrapLogEnter("CMenuBand<%p>::Initialize(IShellMenuCallback *psmc=%p, UINT uId=%u, UINT uIdAncestor=%u, DWORD dwFlags=%08x)\n", this, psmc, uId, uIdAncestor, dwFlags);
-    HRESULT hr = m_IShellMenu->Initialize(psmc, uId, uIdAncestor, dwFlags);
-    WrapLogExit("CMenuBand::Initialize() = %08x\n", hr);
-    return hr;
-}
+    IBandSite* pBandSite;
+    IDeskBar* pDeskBar;
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetMenuInfo(IShellMenuCallback **ppsmc, UINT *puId, UINT *puIdAncestor, DWORD *pdwFlags)
-{
-    WrapLogEnter("CMenuBand<%p>::GetMenuInfo(IShellMenuCallback **ppsmc=%p, UINT *puId=%p, UINT *puIdAncestor=%p, DWORD *pdwFlags=%p)\n", this, ppsmc, puId, puIdAncestor, pdwFlags);
-    HRESULT hr = m_IShellMenu->GetMenuInfo(ppsmc, puId, puIdAncestor, pdwFlags);
-    if (ppsmc) WrapLogMsg("*ppsmc=%p\n", *ppsmc);
-    if (puId) WrapLogMsg("*puId=%u\n", *puId);
-    if (puIdAncestor) WrapLogMsg("*puIdAncestor=%u\n", *puIdAncestor);
-    if (pdwFlags) WrapLogMsg("*pdwFlags=%08x\n", *pdwFlags);
-    WrapLogExit("CMenuBand::GetMenuInfo() = %08x\n", hr);
-    return hr;
-}
-
-HRESULT STDMETHODCALLTYPE CMenuBand::SetShellFolder(IShellFolder *psf, LPCITEMIDLIST pidlFolder, HKEY hKey, DWORD dwFlags)
-{
-    WrapLogEnter("CMenuBand<%p>::SetShellFolder(IShellFolder *psf=%p, LPCITEMIDLIST pidlFolder=%p, HKEY hKey=%p, DWORD dwFlags=%08x)\n", this, psf, pidlFolder, hKey, dwFlags);
-    HRESULT hr = m_IShellMenu->SetShellFolder(psf, pidlFolder, hKey, dwFlags);
-    WrapLogExit("CMenuBand::SetShellFolder() = %08x\n", hr);
-    return hr;
-}
+    HRESULT hr = 0;
+    RECT rc = { 0 };
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REFIID riid, void **ppv)
-{
-    WrapLogEnter("CMenuBand<%p>::GetShellFolder(DWORD *pdwFlags=%p, LPITEMIDLIST *ppidl=%p, REFIID riid=%s, void **ppv=%p)\n", this, pdwFlags, ppidl, Wrap(riid), ppv);
-    HRESULT hr = m_IShellMenu->GetShellFolder(pdwFlags, ppidl, riid, ppv);
-    if (pdwFlags) WrapLogMsg("*pdwFlags=%08x\n", *pdwFlags);
-    if (ppidl) WrapLogMsg("*ppidl=%p\n", *ppidl);
-    if (ppv) WrapLogMsg("*ppv=%p\n", *ppv);
-    WrapLogExit("CMenuBand::GetShellFolder() = %08x\n", hr);
-    return hr;
-}
+    if (!SendMessage(m_hwnd, TB_GETITEMRECT, index, reinterpret_cast<LPARAM>(&rc)))
+        return E_FAIL;
 
-HRESULT STDMETHODCALLTYPE CMenuBand::SetMenu(HMENU hmenu, HWND hwnd, DWORD dwFlags)
-{
-    WrapLogEnter("CMenuBand<%p>::SetMenu(HMENU hmenu=%p, HWND hwnd=%p, DWORD dwFlags=%08x)\n", this, hmenu, hwnd, dwFlags);
-    HRESULT hr = m_IShellMenu->SetMenu(hmenu, hwnd, dwFlags);
-    WrapLogExit("CMenuBand::SetMenu() = %08x\n", hr);
-    return hr;
-}
+    POINT a = { rc.left, rc.top };
+    POINT b = { rc.right, rc.bottom };
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetMenu(HMENU *phmenu, HWND *phwnd, DWORD *pdwFlags)
-{
-    WrapLogEnter("CMenuBand<%p>::GetMenu(HMENU *phmenu=%p, HWND *phwnd=%p, DWORD *pdwFlags=%p)\n", this, phmenu, phwnd, pdwFlags);
-    HRESULT hr = m_IShellMenu->GetMenu(phmenu, phwnd, pdwFlags);
-    if (phmenu) WrapLogMsg("*phmenu=%p\n", *phmenu);
-    if (phwnd) WrapLogMsg("*phwnd=%p\n", *phwnd);
-    if (pdwFlags) WrapLogMsg("*pdwFlags=%08x\n", *pdwFlags);
-    WrapLogExit("CMenuBand::GetMenu() = %08x\n", hr);
-    return hr;
-}
+    ClientToScreen(m_hwnd, &a);
+    ClientToScreen(m_hwnd, &b);
 
-HRESULT STDMETHODCALLTYPE CMenuBand::InvalidateItem(LPSMDATA psmd, DWORD dwFlags)
-{
-    WrapLogEnter("CMenuBand<%p>::InvalidateItem(LPSMDATA psmd=%p, DWORD dwFlags=%08x)\n", this, psmd, dwFlags);
-    HRESULT hr = m_IShellMenu->InvalidateItem(psmd, dwFlags);
-    WrapLogExit("CMenuBand::InvalidateItem() = %08x\n", hr);
-    return hr;
-}
+    POINTL pt = { b.x, b.y };
+    RECTL rcl = { a.x, a.y, b.x, b.y }; // maybe-TODO: fetch client area of deskbar?
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetState(LPSMDATA psmd)
-{
-    WrapLogEnter("CMenuBand<%p>::GetState(LPSMDATA psmd=%p)\n", this, psmd);
-    HRESULT hr = m_IShellMenu->GetState(psmd);
-    WrapLogExit("CMenuBand::GetState() = %08x\n", hr);
-    return hr;
-}
 
-HRESULT STDMETHODCALLTYPE CMenuBand::SetMenuToolbar(IUnknown *punk, DWORD dwFlags)
-{
-    WrapLogEnter("CMenuBand<%p>::SetMenuToolbar(IUnknown *punk=%p, DWORD dwFlags=%08x)\n", this, punk, dwFlags);
-    HRESULT hr = m_IShellMenu->SetMenuToolbar(punk, dwFlags);
-    WrapLogExit("CMenuBand::SetMenuToolbar() = %08x\n", hr);
-    return hr;
-}
+#if USE_SYSTEM_MENUSITE
+    hr = CoCreateInstance(CLSID_MenuBandSite,
+        NULL,
+        CLSCTX_INPROC_SERVER,
+        IID_PPV_ARG(IBandSite, &pBandSite));
 #else
+    hr = CMenuSite_Constructor(IID_PPV_ARG(IBandSite, &pBandSite));
+#endif
+    if (FAILED(hr))
+        return hr;
+#if WRAP_MENUSITE
+    hr = CMenuSite_Wrapper(pBandSite, IID_PPV_ARG(IBandSite, &pBandSite));
+    if (FAILED(hr))
+        return hr;
+#endif
 
-#include <windowsx.h>
-
-#define TBSTYLE_EX_VERTICAL 4
-
-CMenuToolbarBase::CMenuToolbarBase(CMenuBand *menuBand) :
-    m_menuBand(menuBand),
-    m_hwnd(NULL),
-    m_dwMenuFlags(0)
-{
-}
-
-HRESULT CMenuToolbarBase::ShowWindow(BOOL fShow)
-{
-    ::ShowWindow(m_hwnd, fShow ? SW_SHOW : SW_HIDE);
-
-    HIMAGELIST ilBig, ilSmall;
-    Shell_GetImageLists(&ilBig, &ilSmall);
-
-    if (m_menuBand->UseBigIcons())
-    {
-        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, (LPARAM) ilBig);
-    }
-    else
-    {
-        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, (LPARAM) ilSmall);
-    }
-
-    return S_OK;
-}
-
-HRESULT CMenuToolbarBase::Close()
-{
-    DestroyWindow(m_hwnd);
-    m_hwnd = NULL;
-    return S_OK;
-}
-
-HRESULT CMenuToolbarBase::CreateToolbar(HWND hwndParent, DWORD dwFlags)
-{
-    LONG tbStyles = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
-        TBSTYLE_TOOLTIPS | TBSTYLE_TRANSPARENT | TBSTYLE_REGISTERDROP | TBSTYLE_LIST | TBSTYLE_FLAT |
-        CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NORESIZE | CCS_TOP;
-    LONG tbExStyles = TBSTYLE_EX_DOUBLEBUFFER | TBSTYLE_EX_DRAWDDARROWS;
+#if USE_SYSTEM_MENUDESKBAR
+    hr = CoCreateInstance(CLSID_MenuDeskBar,
+        NULL,
+        CLSCTX_INPROC_SERVER,
+        IID_PPV_ARG(IDeskBar, &pDeskBar));
+#else
+    hr = CMenuDeskBar_Constructor(IID_PPV_ARG(IDeskBar, &pDeskBar));
+#endif
+    if (FAILED(hr))
+        return hr;
+#if WRAP_MENUDESKBAR
+    hr = CMenuDeskBar_Wrapper(pDeskBar, IID_PPV_ARG(IDeskBar, &pDeskBar));
+    if (FAILED(hr))
+        return hr;
+#endif
 
-    if (dwFlags & SMINIT_VERTICAL)
-    {
-        tbStyles |= CCS_VERT;
-        tbExStyles |= TBSTYLE_EX_VERTICAL;
-    }
+    hr = pDeskBar->SetClient(pBandSite);
+    if (FAILED(hr))
+        return hr;
 
-    RECT rc;
+    hr = pBandSite->AddBand(childShellMenu);
+    if (FAILED(hr))
+        return hr;
 
-    if (!::GetClientRect(hwndParent, &rc) || (rc.left == rc.right) || (rc.top == rc.bottom))
-    {
-        rc.left = 0;
-        rc.top = 0;
-        rc.right = 1;
-        rc.bottom = 1;
-    }
+    CComPtr<IMenuPopup> popup;
+    hr = pDeskBar->QueryInterface(IID_PPV_ARG(IMenuPopup, &popup));
+    if (FAILED(hr))
+        return hr;
 
-    HWND hwndToolbar = CreateWindowEx(
-        tbExStyles, TOOLBARCLASSNAMEW, NULL,
-        tbStyles, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
-        hwndParent, NULL, _AtlBaseModule.GetModuleInstance(), 0);
+    popup->Popup(&pt, &rcl, MPPF_TOP | MPPF_RIGHT);
 
-    if (hwndToolbar == NULL)
-        return E_FAIL;
+    return S_OK;
+}
 
-    ::SetParent(hwndToolbar, hwndParent);
+HRESULT CMenuToolbarBase::PopupSubMenu(UINT index, HMENU menu)
+{
+    RECT rc = { 0 };
 
-    m_hwnd = hwndToolbar;
+    if (!SendMessage(m_hwnd, TB_GETITEMRECT, index, reinterpret_cast<LPARAM>(&rc)))
+        return E_FAIL;
 
-    /* Identify the version of the used Common Controls DLL by sending the size of the TBBUTTON structure */
-    SendMessageW(m_hwnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
+    POINT b = { rc.right, rc.bottom };
 
-    HIMAGELIST ilBig, ilSmall;
-    Shell_GetImageLists(&ilBig, &ilSmall);
+    ClientToScreen(m_hwnd, &b);
 
-    //if (dwFlags & SMINIT_TOPLEVEL)
-    //{
-    //    /* Hide the placeholders for the button images */
-    //    SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, 0);
-    //}
-    //else
-    if (m_menuBand->UseBigIcons())
-    {
-        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, (LPARAM) ilBig);
-    }
-    else
-    {
-        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, (LPARAM) ilSmall);
-    }
+    HMENU popup = GetSubMenu(menu, index);
+
+    m_menuBand->TrackPopup(popup, b.x, b.y);
 
     return S_OK;
 }
 
-HRESULT CMenuToolbarBase::GetWindow(HWND *phwnd)
+HRESULT CMenuToolbarBase::DoContextMenu(IContextMenu* contextMenu)
 {
-    if (!phwnd)
+    HRESULT hr;
+    HMENU hPopup = CreatePopupMenu();
+
+    if (hPopup == NULL)
         return E_FAIL;
 
-    *phwnd = m_hwnd;
+    hr = contextMenu->QueryContextMenu(hPopup, 0, 0, UINT_MAX, CMF_NORMAL);
+    if (FAILED(hr))
+    {
+        DestroyMenu(hPopup);
+        return hr;
+    }
 
+    DWORD dwPos = GetMessagePos();
+    UINT uCommand = ::TrackPopupMenu(hPopup, TPM_RETURNCMD, GET_X_LPARAM(dwPos), GET_Y_LPARAM(dwPos), 0, m_hwnd, NULL);
+    if (uCommand == 0)
+        return S_FALSE;
+
+    CMINVOKECOMMANDINFO cmi = { 0 };
+    cmi.cbSize = sizeof(cmi);
+    cmi.lpVerb = MAKEINTRESOURCEA(uCommand);
+    cmi.hwnd = m_hwnd;
+    hr = contextMenu->InvokeCommand(&cmi);
+
+    DestroyMenu(hPopup);
+    return hr;
+}
+
+HRESULT CMenuToolbarBase::OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult)
+{
+    m_menuBand->OnSelect(MPOS_EXECUTE);
     return S_OK;
 }
 
@@ -940,14 +611,14 @@ HRESULT  CMenuStaticToolbar::SetMenu(
 HRESULT CMenuStaticToolbar::FillToolbar()
 {
     int i;
-    PWSTR MenuString;
-
     int ic = GetMenuItemCount(m_hmenu);
 
     for (i = 0; i < ic; i++)
     {
         MENUITEMINFOW info;
         TBBUTTON tbb = { 0 };
+        PWSTR MenuString = NULL;
+
         tbb.fsState = TBSTATE_ENABLED;
         tbb.fsStyle = 0;
 
@@ -960,30 +631,95 @@ HRESULT CMenuStaticToolbar::FillToolbar()
         {
             if (!AllocAndGetMenuString(m_hmenu, i, &MenuString))
                 return E_OUTOFMEMORY;
-            tbb.fsStyle |= BTNS_DROPDOWN;
+            if (::GetSubMenu(m_hmenu, i) != NULL)
+                tbb.fsStyle |= BTNS_DROPDOWN;
+            tbb.iString = (INT_PTR) MenuString;
+            tbb.idCommand = info.wID;
+
+            SMINFO * sminfo = new SMINFO();
+            sminfo->dwMask = SMIM_ICON | SMIM_FLAGS;
+            if (SUCCEEDED(m_menuBand->CallCBWithId(info.wID, SMC_GETINFO, 0, reinterpret_cast<LPARAM>(sminfo))))
+            {
+                tbb.iBitmap = sminfo->iIcon;
+                tbb.dwData = reinterpret_cast<DWORD_PTR>(sminfo);
+                // FIXME: remove before deleting the toolbar or it will leak
+            }
         }
         else
         {
-            MenuString = L"";
             tbb.fsStyle |= BTNS_SEP;
         }
 
-        tbb.idCommand = info.wID;
-        tbb.iString = (INT_PTR) MenuString;
-
-        SMINFO sminfo;
-        if (info.wID >= 0 && SUCCEEDED(m_menuBand->CallCBWithId(info.wID, SMC_GETINFO, 0, (LPARAM) &sminfo)))
-        {
-            tbb.iBitmap = sminfo.iIcon;
-        }
+        SendMessageW(m_hwnd, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb));
 
-        SendMessageW(m_hwnd, TB_ADDBUTTONS, 1, (LPARAM) (LPTBBUTTON) &tbb);
-        HeapFree(GetProcessHeap(), 0, MenuString);
+        if (MenuString)
+            HeapFree(GetProcessHeap(), 0, MenuString);
     }
 
     return S_OK;
 }
 
+HRESULT CMenuStaticToolbar::OnContextMenu(NMMOUSE * rclick)
+{
+    CComPtr<IContextMenu> contextMenu;
+    HRESULT hr = m_menuBand->CallCBWithId(rclick->dwItemSpec, SMC_GETOBJECT, reinterpret_cast<WPARAM>(&IID_IContextMenu), reinterpret_cast<LPARAM>(&contextMenu));
+    if (hr != S_OK)
+        return hr;
+
+    return DoContextMenu(contextMenu);
+}
+
+HRESULT CMenuStaticToolbar::OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult)
+{
+    HRESULT hr = m_menuBand->CallCBWithId(wParam, SMC_EXEC, 0, 0);
+    if (FAILED(hr))
+        return hr;
+
+    return CMenuToolbarBase::OnCommand(wParam, lParam, theResult);
+}
+
+HRESULT CMenuStaticToolbar::PopupItem(UINT uItem)
+{
+    TBBUTTONINFO info = { 0 };
+    info.cbSize = sizeof(TBBUTTONINFO);
+    info.dwMask = 0;
+    int index = SendMessage(m_hwnd, TB_GETBUTTONINFO, uItem, reinterpret_cast<LPARAM>(&info));
+    if (index < 0)
+        return E_FAIL;
+    
+    TBBUTTON btn = { 0 };
+    SendMessage(m_hwnd, TB_GETBUTTON, index, reinterpret_cast<LPARAM>(&btn));
+
+    SMINFO * nfo = reinterpret_cast<SMINFO*>(btn.dwData);
+    if (!nfo)
+        return E_FAIL;
+
+    if (nfo->dwFlags&SMIF_TRACKPOPUP)
+    {
+        return PopupSubMenu(index, m_hmenu);
+    }
+    else
+    {
+        CComPtr<IShellMenu> shellMenu;
+        HRESULT hr = m_menuBand->CallCBWithId(uItem, SMC_GETOBJECT, reinterpret_cast<WPARAM>(&IID_IShellMenu), reinterpret_cast<LPARAM>(&shellMenu));
+        if (FAILED(hr))
+            return hr;
+
+        return PopupSubMenu(index, shellMenu);
+    }
+}
+
+HRESULT CMenuStaticToolbar::HasSubMenu(UINT uItem)
+{
+    TBBUTTONINFO info = { 0 };
+    info.cbSize = sizeof(TBBUTTONINFO);
+    info.dwMask = 0;
+    int index = SendMessage(m_hwnd, TB_GETBUTTONINFO, uItem, reinterpret_cast<LPARAM>(&info));
+    if (index < 0)
+        return E_FAIL;
+    return ::GetSubMenu(m_hmenu, index) ? S_OK : S_FALSE;
+}
+
 CMenuSFToolbar::CMenuSFToolbar(CMenuBand * menuBand) :
     CMenuToolbarBase(menuBand),
     m_shellFolder(NULL)
@@ -1007,14 +743,14 @@ HRESULT CMenuSFToolbar::FillToolbar()
     IEnumIDList * eidl;
     m_shellFolder->EnumObjects(m_hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &eidl);
 
-    LPITEMIDLIST item = (LPITEMIDLIST) CoTaskMemAlloc(sizeof(ITEMIDLIST));
+    LPITEMIDLIST item = static_cast<LPITEMIDLIST>(CoTaskMemAlloc(sizeof(ITEMIDLIST)));
     ULONG fetched;
     while ((hr = eidl->Next(1, &item, &fetched)) == S_OK)
     {
         INT index = 0;
         INT indexOpen = 0;
 
-        IShellItem *psi;
+        CComPtr<IShellItem> psi;
         SHCreateShellItem(NULL, m_shellFolder, item, &psi);
 
         hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY, &MenuString);
@@ -1023,11 +759,21 @@ HRESULT CMenuSFToolbar::FillToolbar()
 
         index = SHMapPIDLToSystemImageListIndex(m_shellFolder, item, &indexOpen);
 
+        SFGAOF attrs;
+        hr = psi->GetAttributes(SFGAO_FOLDER, &attrs);
+
+        if (attrs != 0)
+        {
+            tbb.fsStyle |= BTNS_DROPDOWN;
+        }
+
         tbb.idCommand = i++;
         tbb.iString = (INT_PTR) MenuString;
         tbb.iBitmap = index;
+        tbb.dwData = reinterpret_cast<DWORD_PTR>(ILClone(item));
+        // FIXME: remove before deleting the toolbar or it will leak
 
-        SendMessageW(m_hwnd, TB_ADDBUTTONS, 1, (LPARAM) (LPTBBUTTON) &tbb);
+        SendMessageW(m_hwnd, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb));
         HeapFree(GetProcessHeap(), 0, MenuString);
 
     }
@@ -1076,6 +822,107 @@ HRESULT CMenuSFToolbar::GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REF
     return hr;
 }
 
+LPITEMIDLIST CMenuSFToolbar::GetPidlFromId(UINT uItem, INT* pIndex)
+{
+    TBBUTTONINFO info = { 0 };
+    info.cbSize = sizeof(TBBUTTONINFO);
+    info.dwMask = 0;
+    int index = SendMessage(m_hwnd, TB_GETBUTTONINFO, uItem, reinterpret_cast<LPARAM>(&info));
+    if (index < 0)
+        return NULL;
+
+    if (pIndex)
+        *pIndex = index;
+
+    TBBUTTON btn = { 0 };
+    if (!SendMessage(m_hwnd, TB_GETBUTTON, index, reinterpret_cast<LPARAM>(&btn)))
+        return NULL;
+
+    return reinterpret_cast<LPITEMIDLIST>(btn.dwData);
+}
+
+HRESULT CMenuSFToolbar::OnContextMenu(NMMOUSE * rclick)
+{
+    HRESULT hr;
+    CComPtr<IContextMenu> contextMenu;
+    LPCITEMIDLIST pidl = reinterpret_cast<LPCITEMIDLIST>(rclick->dwItemData);
+
+    hr = m_shellFolder->GetUIObjectOf(m_hwnd, 1, &pidl, IID_IContextMenu, NULL, reinterpret_cast<VOID **>(&contextMenu));
+    if (hr != S_OK)
+        return hr;
+
+    return DoContextMenu(contextMenu);
+}
+
+HRESULT CMenuSFToolbar::OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult)
+{
+    return m_menuBand->CallCBWithPidl(GetPidlFromId(wParam, NULL), SMC_SFEXEC, 0, 0);
+}
+
+HRESULT CMenuSFToolbar::PopupItem(UINT uItem)
+{
+    HRESULT hr;
+    UINT uId;
+    UINT uIdAncestor;
+    DWORD flags;
+    int index;
+    CComPtr<IShellMenuCallback> psmc;
+    CComPtr<IShellMenu> shellMenu;
+    
+    LPITEMIDLIST pidl = GetPidlFromId(uItem, &index);
+
+    if (!pidl)
+        return E_FAIL;
+
+#if USE_SYSTEM_MENUBAND
+    hr = CoCreateInstance(CLSID_MenuBand,
+        NULL,
+        CLSCTX_INPROC_SERVER,
+        IID_PPV_ARG(IShellMenu, &shellMenu));
+#else
+    hr = CMenuBand_Constructor(IID_PPV_ARG(IShellMenu, &shellMenu));
+#endif
+    if (FAILED(hr))
+        return hr;
+#if WRAP_MENUBAND
+    hr = CMenuBand_Wrapper(shellMenu, IID_PPV_ARG(IShellMenu, &shellMenu));
+    if (FAILED(hr))
+        return hr;
+#endif
+
+    m_menuBand->GetMenuInfo(&psmc, &uId, &uIdAncestor, &flags);
+
+    // FIXME: not sure waht to use as uId/uIdAncestor here
+    hr = shellMenu->Initialize(psmc, 0, uId, SMINIT_VERTICAL);
+    if (FAILED(hr))
+        return hr;
+
+    CComPtr<IShellFolder> childFolder;
+    hr = m_shellFolder->BindToObject(pidl, NULL, IID_PPV_ARG(IShellFolder, &childFolder));
+    if (FAILED(hr))
+        return hr;
+
+    hr = shellMenu->SetShellFolder(childFolder, NULL, NULL, 0);
+    if (FAILED(hr))
+        return hr;
+
+    return PopupSubMenu(index, shellMenu);
+}
+
+HRESULT CMenuSFToolbar::HasSubMenu(UINT uItem)
+{
+    HRESULT hr;
+    CComPtr<IShellItem> psi;
+    SHCreateShellItem(NULL, m_shellFolder, GetPidlFromId(uItem, NULL), &psi);
+
+    SFGAOF attrs;
+    hr = psi->GetAttributes(SFGAO_FOLDER, &attrs);
+    if (FAILED(hr))
+        return hr;
+
+    return (attrs != 0) ? S_OK : S_FALSE;
+}
+
 CMenuBand::CMenuBand() :
     m_site(NULL),
     m_psmc(NULL),
@@ -1118,7 +965,7 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::Initialize(
     {
         m_psmc->AddRef();
 
-        _CallCB(SMC_CREATE, 0, (LPARAM) &m_UserData);
+        _CallCB(SMC_CREATE, 0, reinterpret_cast<LPARAM>(&m_UserData));
     }
 
     return S_OK;
@@ -1157,27 +1004,28 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::SetMenu(
         m_staticToolbar = new CMenuStaticToolbar(this);
     }
     m_hmenu = hmenu;
+    m_menuOwner;
 
-    HRESULT hResult = m_staticToolbar->SetMenu(hmenu, hwnd, dwFlags);
-    if (FAILED(hResult))
-        return hResult;
+    HRESULT hr = m_staticToolbar->SetMenu(hmenu, hwnd, dwFlags);
+    if (FAILED(hr))
+        return hr;
 
     if (m_site)
     {
         HWND hwndParent;
 
-        hResult = m_site->GetWindow(&hwndParent);
-        if (FAILED(hResult))
-            return hResult;
+        hr = m_site->GetWindow(&hwndParent);
+        if (FAILED(hr))
+            return hr;
 
-        hResult = m_staticToolbar->CreateToolbar(hwndParent, m_dwFlags);
-        if (FAILED(hResult))
-            return hResult;
+        hr = m_staticToolbar->CreateToolbar(hwndParent, m_dwFlags);
+        if (FAILED(hr))
+            return hr;
 
-        hResult = m_staticToolbar->FillToolbar();
+        hr = m_staticToolbar->FillToolbar();
     }
 
-    return hResult;
+    return hr;
 }
 
 HRESULT STDMETHODCALLTYPE  CMenuBand::GetMenu(
@@ -1193,8 +1041,8 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::GetMenu(
 
 HRESULT STDMETHODCALLTYPE  CMenuBand::SetSite(IUnknown *pUnkSite)
 {
-    HWND                    hwndParent;
-    HRESULT                 hResult;
+    HWND    hwndParent;
+    HRESULT hr;
 
     if (m_site != NULL)
         m_site->Release();
@@ -1203,34 +1051,47 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::SetSite(IUnknown *pUnkSite)
         return S_OK;
 
     hwndParent = NULL;
-    hResult = pUnkSite->QueryInterface(IID_PPV_ARG(IOleWindow, &m_site));
-    if (SUCCEEDED(hResult))
+    hr = pUnkSite->QueryInterface(IID_PPV_ARG(IOleWindow, &m_site));
+    if (SUCCEEDED(hr))
     {
-        m_site->GetWindow(&hwndParent);
         m_site->Release();
+
+        hr = m_site->GetWindow(&hwndParent);
+        if (FAILED(hr))
+            return hr;
     }
+
     if (!::IsWindow(hwndParent))
         return E_FAIL;
 
     if (m_staticToolbar != NULL)
     {
-        hResult = m_staticToolbar->CreateToolbar(hwndParent, m_dwFlags);
-        if (FAILED(hResult))
-            return hResult;
+        hr = m_staticToolbar->CreateToolbar(hwndParent, m_dwFlags);
+        if (FAILED(hr))
+            return hr;
 
-        hResult = m_staticToolbar->FillToolbar();
+        hr = m_staticToolbar->FillToolbar();
+        if (FAILED(hr))
+            return hr;
     }
 
     if (m_SFToolbar != NULL)
     {
-        hResult = m_SFToolbar->CreateToolbar(hwndParent, m_dwFlags);
-        if (FAILED(hResult))
-            return hResult;
+        hr = m_SFToolbar->CreateToolbar(hwndParent, m_dwFlags);
+        if (FAILED(hr))
+            return hr;
 
-        hResult = m_SFToolbar->FillToolbar();
+        hr = m_SFToolbar->FillToolbar();
+        if (FAILED(hr))
+            return hr;
     }
 
-    return S_OK;
+    CComPtr<IOleWindow> pTopLevelWindow;
+    hr = IUnknown_QueryService(m_site, SID_STopLevelBrowser, IID_PPV_ARG(IOleWindow, &pTopLevelWindow));
+    if (FAILED(hr))
+        return hr;
+
+    return pTopLevelWindow->GetWindow(&m_topLevelWindow);
 }
 
 HRESULT STDMETHODCALLTYPE  CMenuBand::GetSite(REFIID riid, PVOID *ppvSite)
@@ -1255,31 +1116,27 @@ HRESULT STDMETHODCALLTYPE CMenuBand::GetWindow(
 
 HRESULT STDMETHODCALLTYPE CMenuBand::OnPosRectChangeDB(RECT *prc)
 {
-    SIZE sizeStaticX = { 0 };
-    SIZE sizeShlFldX = { 0 };
     SIZE sizeStaticY = { 0 };
     SIZE sizeShlFldY = { 0 };
     HWND hwndStatic = NULL;
     HWND hwndShlFld = NULL;
-    HRESULT hResult = S_OK;
+    HRESULT hr = S_OK;
 
     if (m_staticToolbar != NULL)
-        hResult = m_staticToolbar->GetWindow(&hwndStatic);
-    if (FAILED(hResult))
-        return hResult;
+        hr = m_staticToolbar->GetWindow(&hwndStatic);
+    if (FAILED(hr))
+        return hr;
 
     if (m_SFToolbar != NULL)
-        hResult = m_SFToolbar->GetWindow(&hwndShlFld);
-    if (FAILED(hResult))
-        return hResult;
+        hr = m_SFToolbar->GetWindow(&hwndShlFld);
+    if (FAILED(hr))
+        return hr;
 
     if (hwndStatic == NULL && hwndShlFld == NULL)
         return E_FAIL;
 
-    if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeStaticY);
-    if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeShlFldY);
-    if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, FALSE, (LPARAM) &sizeStaticX);
-    if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, FALSE, (LPARAM) &sizeShlFldX);
+    if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, TRUE, reinterpret_cast<LPARAM>(&sizeStaticY));
+    if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, TRUE, reinterpret_cast<LPARAM>(&sizeShlFldY));
 
     int sy = max(prc->bottom - prc->top, sizeStaticY.cy + sizeShlFldY.cy);
 
@@ -1316,17 +1173,17 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::GetBandInfo(
 {
     HWND hwndStatic = NULL;
     HWND hwndShlFld = NULL;
-    HRESULT hResult = S_OK;
+    HRESULT hr = S_OK;
 
     if (m_staticToolbar != NULL)
-        hResult = m_staticToolbar->GetWindow(&hwndStatic);
-    if (FAILED(hResult))
-        return hResult;
+        hr = m_staticToolbar->GetWindow(&hwndStatic);
+    if (FAILED(hr))
+        return hr;
 
     if (m_SFToolbar != NULL)
-        hResult = m_SFToolbar->GetWindow(&hwndShlFld);
-    if (FAILED(hResult))
-        return hResult;
+        hr = m_SFToolbar->GetWindow(&hwndShlFld);
+    if (FAILED(hr))
+        return hr;
 
     if (hwndStatic == NULL && hwndShlFld == NULL)
         return E_FAIL;
@@ -1342,8 +1199,8 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::GetBandInfo(
         SIZE sizeStatic = { 0 };
         SIZE sizeShlFld = { 0 };
 
-        if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeStatic);
-        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeShlFld);
+        if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, TRUE, reinterpret_cast<LPARAM>(&sizeStatic));
+        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, TRUE, reinterpret_cast<LPARAM>(&sizeShlFld));
 
         pdbi->ptMinSize.x = 0;
         pdbi->ptMinSize.y = sizeStatic.cy + sizeShlFld.cy;
@@ -1353,10 +1210,10 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::GetBandInfo(
         SIZE sizeStatic = { 0 };
         SIZE sizeShlFld = { 0 };
 
-        if (hwndStatic) SendMessageW(hwndStatic, TB_GETMAXSIZE, 0, (LPARAM) &sizeStatic);
-        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETMAXSIZE, 0, (LPARAM) &sizeShlFld);
+        if (hwndStatic) SendMessageW(hwndStatic, TB_GETMAXSIZE, 0, reinterpret_cast<LPARAM>(&sizeStatic));
+        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETMAXSIZE, 0, reinterpret_cast<LPARAM>(&sizeShlFld));
 
-        pdbi->ptMaxSize.x = sizeStatic.cx + sizeShlFld.cx; // ignored
+        pdbi->ptMaxSize.x = max(sizeStatic.cx, sizeShlFld.cx); // ignored
         pdbi->ptMaxSize.y = sizeStatic.cy + sizeShlFld.cy;
     }
     if (pdbi->dwMask & DBIM_INTEGRAL)
@@ -1369,12 +1226,12 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::GetBandInfo(
         SIZE sizeStatic = { 0 };
         SIZE sizeShlFld = { 0 };
 
-        if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, FALSE, (LPARAM) &sizeStatic);
-        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, FALSE, (LPARAM) &sizeShlFld);
-        pdbi->ptActual.x = sizeStatic.cx + sizeShlFld.cx;
+        if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, FALSE, reinterpret_cast<LPARAM>(&sizeStatic));
+        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, FALSE, reinterpret_cast<LPARAM>(&sizeShlFld));
+        pdbi->ptActual.x = max(sizeStatic.cx, sizeShlFld.cx);
 
-        if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeStatic);
-        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeShlFld);
+        if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, TRUE, reinterpret_cast<LPARAM>(&sizeStatic));
+        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, TRUE, reinterpret_cast<LPARAM>(&sizeShlFld));
         pdbi->ptActual.y = sizeStatic.cy + sizeShlFld.cy;
     }
     if (pdbi->dwMask & DBIM_TITLE)
@@ -1508,7 +1365,9 @@ HRESULT STDMETHODCALLTYPE CMenuBand::Exec(const GUID *pguidCmdGroup, DWORD nCmdI
 
 HRESULT STDMETHODCALLTYPE CMenuBand::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
 {
-    if (IsEqualIID(guidService, SID_SMenuBandChild))
+    if (IsEqualIID(guidService, SID_SMenuBandChild) ||
+        IsEqualIID(guidService, SID_SMenuBandBottom) || 
+        IsEqualIID(guidService, SID_SMenuBandBottomSelected))
         return this->QueryInterface(riid, ppvObject);
     WARN("Unknown service requested %s\n", wine_dbgstr_guid(&guidService));
     return E_NOINTERFACE;
@@ -1522,7 +1381,11 @@ HRESULT STDMETHODCALLTYPE CMenuBand::Popup(POINTL *ppt, RECTL *prcExclude, MP_PO
 
 HRESULT STDMETHODCALLTYPE CMenuBand::OnSelect(DWORD dwSelectType)
 {
-    UNIMPLEMENTED;
+    CComPtr<IMenuPopup> pmp;
+    HRESULT hr = IUnknown_QueryService(m_site, SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &pmp));
+    if (FAILED(hr))
+        return hr;
+    pmp->OnSelect(dwSelectType);
     return S_OK;
 }
 
@@ -1565,26 +1428,26 @@ HRESULT STDMETHODCALLTYPE CMenuBand::SetShellFolder(IShellFolder *psf, LPCITEMID
         m_SFToolbar = new CMenuSFToolbar(this);
     }
 
-    HRESULT hResult = m_SFToolbar->SetShellFolder(psf, pidlFolder, hKey, dwFlags);
-    if (FAILED(hResult))
-        return hResult;
+    HRESULT hr = m_SFToolbar->SetShellFolder(psf, pidlFolder, hKey, dwFlags);
+    if (FAILED(hr))
+        return hr;
 
     if (m_site)
     {
         HWND hwndParent;
 
-        hResult = m_site->GetWindow(&hwndParent);
-        if (FAILED(hResult))
-            return hResult;
+        hr = m_site->GetWindow(&hwndParent);
+        if (FAILED(hr))
+            return hr;
 
-        hResult = m_SFToolbar->CreateToolbar(hwndParent, m_dwFlags);
-        if (FAILED(hResult))
-            return hResult;
+        hr = m_SFToolbar->CreateToolbar(hwndParent, m_dwFlags);
+        if (FAILED(hr))
+            return hr;
 
-        hResult = m_SFToolbar->FillToolbar();
+        hr = m_SFToolbar->FillToolbar();
     }
 
-    return hResult;
+    return hr;
 }
 
 HRESULT STDMETHODCALLTYPE CMenuBand::GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REFIID riid, void **ppv)
@@ -1615,14 +1478,60 @@ HRESULT STDMETHODCALLTYPE CMenuBand::SetMenuToolbar(IUnknown *punk, DWORD dwFlag
 HRESULT STDMETHODCALLTYPE CMenuBand::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *theResult)
 {
     *theResult = 0;
-    if (uMsg == WM_NOTIFY)
+    switch (uMsg)
     {
-        NMHDR * hdr = (LPNMHDR) lParam;
+    case WM_COMMAND:
+
+        if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd))
+        {
+            return m_staticToolbar->OnCommand(wParam, lParam, theResult);
+        }
+
+        if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd))
+        {
+            return m_SFToolbar->OnCommand(wParam, lParam, theResult);
+        }
+
+        return S_OK;
+
+    case WM_NOTIFY:
+        NMHDR * hdr = reinterpret_cast<LPNMHDR>(lParam);
         NMTBCUSTOMDRAW * cdraw;
+        NMTBHOTITEM * hot;
+        NMMOUSE * rclick;
         switch (hdr->code)
         {
+        case TBN_HOTITEMCHANGE:
+            hot = reinterpret_cast<LPNMTBHOTITEM>(hdr);
+
+            if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd))
+            {
+                return m_staticToolbar->OnHotItemChange(hot);
+            }
+
+            if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd))
+            {
+                return m_SFToolbar->OnHotItemChange(hot);
+            }
+
+            return S_OK;
+
+        case NM_RCLICK:
+            rclick = reinterpret_cast<LPNMMOUSE>(hdr);
+
+            if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd))
+            {
+                return m_staticToolbar->OnContextMenu(rclick);
+            }
+
+            if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd))
+            {
+                return m_SFToolbar->OnContextMenu(rclick);
+            }
+
+            return S_OK;
         case NM_CUSTOMDRAW:
-            cdraw = (LPNMTBCUSTOMDRAW) hdr;
+            cdraw = reinterpret_cast<LPNMTBCUSTOMDRAW>(hdr);
             switch (cdraw->nmcd.dwDrawStage)
             {
             case CDDS_PREPAINT:
@@ -1655,37 +1564,23 @@ HRESULT STDMETHODCALLTYPE CMenuBand::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wPa
                     break;
                 }
 
-                *theResult = TBCDRF_NOBACKGROUND | TBCDRF_NOEDGES | TBCDRF_NOETCHEDEFFECT | TBCDRF_HILITEHOTTRACK;
+                *theResult = TBCDRF_NOBACKGROUND | TBCDRF_NOEDGES | TBCDRF_NOETCHEDEFFECT | TBCDRF_HILITEHOTTRACK | TBCDRF_NOOFFSET;
                 return S_OK;
             }
             return S_OK;
         }
         return S_OK;
-       
     }
+
     return S_FALSE;
 }
 
 HRESULT STDMETHODCALLTYPE CMenuBand::IsWindowOwner(HWND hWnd)
 {
-    HWND hwndStatic = NULL;
-    HWND hwndShlFld = NULL;
-    HRESULT hr;
-
-    if (m_staticToolbar != NULL)
-        hr = m_staticToolbar->GetWindow(&hwndStatic);
-    if (FAILED(hr))
-        return hr;
-
-    if (hWnd == hwndStatic)
+    if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd))
         return S_OK;
 
-    if (m_SFToolbar != NULL)
-        hr = m_SFToolbar->GetWindow(&hwndShlFld);
-    if (FAILED(hr))
-        return hr;
-
-    if (hWnd == hwndShlFld)
+    if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd))
         return S_OK;
 
     return S_FALSE;
@@ -1763,35 +1658,17 @@ HRESULT STDMETHODCALLTYPE CMenuBand::IsEmpty(THIS)
     return S_OK;
 }
 
-HRESULT CMenuBand::CallCBWithId(UINT Id, UINT uMsg, WPARAM wParam, LPARAM lParam)
+HRESULT CMenuBand::CallCBWithId(UINT id, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
-    if (!m_psmc)
-        return S_FALSE;
-
-    HWND hwnd;
-    GetWindow(&hwnd);
+    return _CallCB(uMsg, wParam, lParam, id);
+}
 
-    SMDATA smData = { 0 };
-    smData.punk = (IShellMenu2*)this;
-    smData.uId = Id;
-    smData.uIdParent = m_uId;
-    smData.uIdAncestor = m_uIdAncestor;
-    smData.hwnd = hwnd;
-    if (m_staticToolbar)
-    {
-        smData.hmenu = m_hmenu;
-    }
-    smData.pvUserData = NULL;
-    if (m_SFToolbar)
-        m_SFToolbar->GetShellFolder(NULL, &smData.pidlFolder, IID_PPV_ARG(IShellFolder, &smData.psf));
-    HRESULT hr = m_psmc->CallbackSM(&smData, uMsg, wParam, lParam);
-    ILFree(smData.pidlFolder);
-    if (smData.psf)
-        smData.psf->Release();
-    return hr;
+HRESULT CMenuBand::CallCBWithPidl(LPITEMIDLIST pidl, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    return _CallCB(uMsg, wParam, lParam, 0, pidl);
 }
 
-HRESULT CMenuBand::_CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam)
+HRESULT CMenuBand::_CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam, UINT id, LPITEMIDLIST pidl)
 {
     if (!m_psmc)
         return S_FALSE;
@@ -1800,10 +1677,12 @@ HRESULT CMenuBand::_CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam)
     GetWindow(&hwnd);
 
     SMDATA smData = { 0 };
-    smData.punk = (IShellMenu2*)this;
+    smData.punk = static_cast<IShellMenu2*>(this);
+    smData.uId = id;
     smData.uIdParent = m_uId;
     smData.uIdAncestor = m_uIdAncestor;
     smData.hwnd = hwnd;
+    smData.pidlItem = pidl;
     if (m_staticToolbar)
     {
         smData.hmenu = m_hmenu;
@@ -1818,4 +1697,8 @@ HRESULT CMenuBand::_CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam)
     return hr;
 }
 
-#endif
\ No newline at end of file
+HRESULT CMenuBand::TrackPopup(HMENU popup, INT x, INT y)
+{
+    ::TrackPopupMenu(popup, 0, x, y, 0, m_menuOwner, NULL);
+    return S_OK;
+}