[RSHELL]
[reactos.git] / base / shell / rshell / CMenuBand.cpp
index 49c3c67..fe996ef 100644 (file)
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
 #include "precomp.h"
-#include "wraplog.h"
+#include <windowsx.h>
+#include <CommonControls.h>
+#include <shlwapi_undoc.h>
+
+extern "C"
+HRESULT WINAPI SHGetImageList(
+    _In_   int iImageList,
+    _In_   REFIID riid,
+    _Out_  void **ppv
+    );
+
+
+#define TBSTYLE_EX_VERTICAL 4
 
 WINE_DEFAULT_DEBUG_CHANNEL(CMenuBand);
 
-#define WRAP_LOG 0
+#define TIMERID_HOTTRACK 1
+#define SUBCLASS_ID_MENUBAND 1
 
 extern "C" BOOL WINAPI Shell_GetImageLists(HIMAGELIST * lpBigList, HIMAGELIST * lpSmallList);
 
 class CMenuBand;
+class CMenuFocusManager;
 
 class CMenuToolbarBase
 {
+private:
+    HWND        m_hwnd;        // May be the pager
+
+protected:
+    CMenuBand * m_menuBand;
+    HWND        m_hwndToolbar;
+    DWORD       m_dwMenuFlags;
+    INT         m_hotItem;
+    WNDPROC     m_SubclassOld;
+    BOOL        m_hasIdealSize;
+    SIZE        m_idealSize;
+    BOOL        m_usePager;
+
+private:
+    static LRESULT CALLBACK s_SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
+
 public:
-    CMenuToolbarBase(CMenuBand *menuBand);
+    CMenuToolbarBase(CMenuBand *menuBand, BOOL usePager);
     virtual ~CMenuToolbarBase() {}
 
+    HRESULT IsWindowOwner(HWND hwnd);
     HRESULT CreateToolbar(HWND hwndParent, DWORD dwFlags);
     HRESULT GetWindow(HWND *phwnd);
     HRESULT ShowWindow(BOOL fShow);
     HRESULT Close();
 
     virtual HRESULT FillToolbar() = 0;
+    virtual HRESULT PopupItem(UINT uItem) = 0;
+    virtual HRESULT HasSubMenu(UINT uItem) = 0;
+    virtual HRESULT OnContextMenu(NMMOUSE * rclick) = 0;
+    virtual HRESULT OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult);
 
-protected:
+    HRESULT PopupSubMenu(UINT itemId, UINT index, IShellMenu* childShellMenu);
+    HRESULT PopupSubMenu(UINT index, HMENU menu);
+    HRESULT DoContextMenu(IContextMenu* contextMenu);
+
+    HRESULT ChangeHotItem(DWORD changeType);
+    HRESULT OnHotItemChange(const NMTBHOTITEM * hot);
 
-    static const UINT WM_USER_SHOWPOPUPMENU = WM_USER + 1;
+    HRESULT GetIdealSize(SIZE& size);
+    HRESULT SetPosSize(int x, int y, int cx, int cy);
 
-    CMenuBand *m_menuBand;
-    HWND m_hwnd;
-    DWORD m_dwMenuFlags;
+    void InvalidateDraw();
+
+protected:
+    LRESULT CALLBACK SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 };
 
-class CMenuStaticToolbar : public CMenuToolbarBase
+class CMenuStaticToolbar :
+    public CMenuToolbarBase
 {
+private:
+    HMENU m_hmenu;
+
 public:
     CMenuStaticToolbar(CMenuBand *menuBand);
     virtual ~CMenuStaticToolbar() {}
@@ -60,13 +106,21 @@ 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;
 };
 
-class CMenuSFToolbar : public CMenuToolbarBase
+class CMenuSFToolbar :
+    public CMenuToolbarBase
 {
+private:
+    IShellFolder * m_shellFolder;
+    LPCITEMIDLIST  m_idList;
+    HKEY           m_hKey;
+
 public:
     CMenuSFToolbar(CMenuBand *menuBand);
     virtual ~CMenuSFToolbar();
@@ -75,12 +129,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:
-
-    IShellFolder * m_shellFolder;
-    LPCITEMIDLIST m_idList;
-    HKEY m_hKey;
+    LPITEMIDLIST GetPidlFromId(UINT uItem, INT* pIndex = NULL);
 };
 
 class CMenuBand :
@@ -98,46 +153,57 @@ class CMenuBand :
     public IWinEventHandler,
     public IShellMenuAcc
 {
-public:
-    CMenuBand();
-    ~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;
+    CMenuFocusManager  * m_focusManager;
+    CMenuStaticToolbar * m_staticToolbar;
+    CMenuSFToolbar     * m_SFToolbar;
 
-    CMenuStaticToolbar *m_staticToolbar;
-    CMenuSFToolbar *m_SFToolbar;
+    CComPtr<IOleWindow>         m_site;
+    CComPtr<IShellMenuCallback> m_psmc;
+    CComPtr<IMenuPopup>         m_subMenuChild;
+    CComPtr<IMenuPopup>         m_subMenuParent;
 
-    UINT m_uId;
-    UINT m_uIdAncestor;
+    UINT  m_uId;
+    UINT  m_uIdAncestor;
     DWORD m_dwFlags;
     PVOID m_UserData;
     HMENU m_hmenu;
-#endif
+    HWND  m_menuOwner;
 
     BOOL m_useBigIcons;
+    HWND m_topLevelWindow;
+
+    CMenuToolbarBase * m_hotBar;
+    INT                m_hotItem;
+    INT                m_popupItem;
+
+    HFONT m_marlett;
 
 public:
+    CMenuBand();
+    ~CMenuBand();
+
+    DECLARE_NOT_AGGREGATABLE(CMenuBand)
+    DECLARE_PROTECT_FINAL_CONSTRUCT()
+
+    BEGIN_COM_MAP(CMenuBand)
+        COM_INTERFACE_ENTRY_IID(IID_IDeskBar, IMenuPopup)
+        COM_INTERFACE_ENTRY_IID(IID_IShellMenu, IShellMenu)
+        COM_INTERFACE_ENTRY_IID(IID_IOleCommandTarget, IOleCommandTarget)
+        COM_INTERFACE_ENTRY_IID(IID_IOleWindow, IDeskBand)
+        COM_INTERFACE_ENTRY_IID(IID_IDockingWindow, IDockingWindow)
+        COM_INTERFACE_ENTRY_IID(IID_IDeskBand, IDeskBand)
+        COM_INTERFACE_ENTRY_IID(IID_IObjectWithSite, IObjectWithSite)
+        COM_INTERFACE_ENTRY_IID(IID_IInputObject, IInputObject)
+        COM_INTERFACE_ENTRY_IID(IID_IPersistStream, IPersistStream)
+        COM_INTERFACE_ENTRY_IID(IID_IPersist, IPersistStream)
+        COM_INTERFACE_ENTRY_IID(IID_IServiceProvider, IServiceProvider)
+        COM_INTERFACE_ENTRY_IID(IID_IMenuPopup, IMenuPopup)
+        COM_INTERFACE_ENTRY_IID(IID_IMenuBand, IMenuBand)
+        COM_INTERFACE_ENTRY_IID(IID_IShellMenu2, IShellMenu2)
+        COM_INTERFACE_ENTRY_IID(IID_IWinEventHandler, IWinEventHandler)
+        COM_INTERFACE_ENTRY_IID(IID_IShellMenuAcc, IShellMenuAcc)
+    END_COM_MAP()
 
     // *** IDeskBand methods ***
     virtual HRESULT STDMETHODCALLTYPE GetBandInfo(DWORD dwBandID, DWORD dwViewMode, DESKBANDINFO *pdbi);
@@ -221,38 +287,277 @@ public:
     virtual HRESULT STDMETHODCALLTYPE DoDefaultAction(THIS);
     virtual HRESULT STDMETHODCALLTYPE IsEmpty(THIS);
 
-    HRESULT CallCBWithId(UINT Id, UINT uMsg, WPARAM wParam, LPARAM lParam);
+    HRESULT _CallCBWithItemId(UINT Id, UINT uMsg, WPARAM wParam, LPARAM lParam);
+    HRESULT _CallCBWithItemPidl(LPITEMIDLIST pidl, UINT uMsg, WPARAM wParam, LPARAM lParam);
+    HRESULT _TrackSubMenuUsingTrackPopupMenu(HMENU popup, INT x, INT y);
+    HRESULT _GetTopLevelWindow(HWND*topLevel);
+    HRESULT _OnHotItemChanged(CMenuToolbarBase * tb, INT id);
+    HRESULT _MenuItemHotTrack(DWORD changeType);
+    HRESULT _OnPopupSubMenu(INT popupItem, IMenuPopup * popup, POINTL * pAt, RECTL * pExclude);
 
-    BOOL UseBigIcons() {
+    BOOL UseBigIcons()
+    {
         return m_useBigIcons;
     }
 
-    DECLARE_NOT_AGGREGATABLE(CMenuBand)
-    DECLARE_PROTECT_FINAL_CONSTRUCT()
+private:
+    HRESULT _CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam, UINT id = 0, LPITEMIDLIST pidl = NULL);
+};
 
-    BEGIN_COM_MAP(CMenuBand)
-        COM_INTERFACE_ENTRY_IID(IID_IDeskBar, IMenuPopup)
-        COM_INTERFACE_ENTRY_IID(IID_IShellMenu, IShellMenu)
-        COM_INTERFACE_ENTRY_IID(IID_IOleCommandTarget, IOleCommandTarget)
-        COM_INTERFACE_ENTRY_IID(IID_IOleWindow, IDeskBand)
-        COM_INTERFACE_ENTRY_IID(IID_IDockingWindow, IDockingWindow)
-        COM_INTERFACE_ENTRY_IID(IID_IDeskBand, IDeskBand)
-        COM_INTERFACE_ENTRY_IID(IID_IObjectWithSite, IObjectWithSite)
-        COM_INTERFACE_ENTRY_IID(IID_IInputObject, IInputObject)
-        COM_INTERFACE_ENTRY_IID(IID_IPersistStream, IPersistStream)
-        COM_INTERFACE_ENTRY_IID(IID_IPersist, IPersistStream)
-        COM_INTERFACE_ENTRY_IID(IID_IServiceProvider, IServiceProvider)
-        COM_INTERFACE_ENTRY_IID(IID_IMenuPopup, IMenuPopup)
-        COM_INTERFACE_ENTRY_IID(IID_IMenuBand, IMenuBand)
-        COM_INTERFACE_ENTRY_IID(IID_IShellMenu2, IShellMenu2)
-        COM_INTERFACE_ENTRY_IID(IID_IWinEventHandler, IWinEventHandler)
-        COM_INTERFACE_ENTRY_IID(IID_IShellMenuAcc, IShellMenuAcc)
-    END_COM_MAP()
+class CMenuFocusManager :
+    public CComCoClass<CMenuFocusManager>,
+    public CComObjectRootEx<CComMultiThreadModelNoCS>
+{
+private:
+    static DWORD TlsIndex;
+
+    static CMenuFocusManager * GetManager()
+    {
+        return reinterpret_cast<CMenuFocusManager *>(TlsGetValue(TlsIndex));
+    }
+
+public:
+    static CMenuFocusManager * AcquireManager()
+    {
+        CMenuFocusManager * obj = NULL;
+
+        if (!TlsIndex)
+        {
+            if ((TlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
+                return NULL;
+        }
+
+        obj = GetManager();
+
+        if (!obj)
+        {
+            obj = new CComObject<CMenuFocusManager>();
+            TlsSetValue(TlsIndex, obj);
+        }
+
+        obj->AddRef();
+
+        return obj;
+    }
+
+    static void ReleaseManager(CMenuFocusManager * obj)
+    {
+        if (!obj->Release())
+        {
+            TlsSetValue(TlsIndex, NULL);
+        }
+    }
 
 private:
-    HRESULT _CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam);
+    static LRESULT CALLBACK s_GetMsgHook(INT nCode, WPARAM wParam, LPARAM lParam)
+    {
+        return GetManager()->GetMsgHook(nCode, wParam, lParam);
+    }
+
+private:
+    CMenuBand * m_currentBand;
+    HWND m_currentFocus;
+    HHOOK m_hHook;
+    DWORD m_threadId;
+
+    // TODO: make dynamic
+#define MAX_RECURSE 20
+    CMenuBand* m_bandStack[MAX_RECURSE];
+    int m_bandCount;
+
+    HRESULT PushToArray(CMenuBand * item)
+    {
+        if (m_bandCount >= MAX_RECURSE)
+            return E_OUTOFMEMORY;
+
+        m_bandStack[m_bandCount++] = item;
+        return S_OK;
+    }
+
+    HRESULT PopFromArray(CMenuBand ** pItem)
+    {
+        if (pItem)
+            *pItem = NULL;
+
+        if (m_bandCount <= 0)
+            return E_FAIL;
+
+        m_bandCount--;
+
+        if (pItem)
+            *pItem = m_bandStack[m_bandCount];
+
+        m_bandStack[m_bandCount] = NULL;
+
+        return S_OK;
+    }
+
+    HRESULT PeekArray(CMenuBand ** pItem)
+    {
+        if (!pItem)
+            return E_FAIL;
+
+        *pItem = NULL;
+
+        if (m_bandCount <= 0)
+            return E_FAIL;
+
+        *pItem = m_bandStack[m_bandCount - 1];
+
+        return S_OK;
+    }
+
+protected:
+    CMenuFocusManager() :
+        m_currentBand(NULL),
+        m_currentFocus(NULL),
+        m_bandCount(0)
+    {
+        m_threadId = GetCurrentThreadId();
+    }
+
+    ~CMenuFocusManager()
+    {
+    }
+
+public:
+
+    DECLARE_NOT_AGGREGATABLE(CMenuFocusManager)
+    DECLARE_PROTECT_FINAL_CONSTRUCT()
+    BEGIN_COM_MAP(CMenuFocusManager)
+    END_COM_MAP()
+
+    LRESULT GetMsgHook(INT nCode, WPARAM wParam, LPARAM lParam)
+    {
+        if (nCode < 0)
+            return CallNextHookEx(m_hHook, nCode, wParam, lParam);
+
+        if (nCode == HC_ACTION)
+        {
+            BOOL callNext = TRUE;
+            MSG* msg = reinterpret_cast<MSG*>(lParam);
+
+            // Do whatever is necessary here
+
+            switch (msg->message)
+            {
+            case WM_CLOSE:
+                break;
+            case WM_SYSKEYDOWN:
+            case WM_KEYDOWN:
+                switch (msg->wParam)
+                {
+                case VK_MENU:
+                case VK_LMENU:
+                case VK_RMENU:
+                    m_currentBand->_MenuItemHotTrack(MPOS_FULLCANCEL);
+                    break;
+                case VK_LEFT:
+                    m_currentBand->_MenuItemHotTrack(MPOS_SELECTLEFT);
+                    break;
+                case VK_RIGHT:
+                    m_currentBand->_MenuItemHotTrack(MPOS_SELECTRIGHT);
+                    break;
+                case VK_UP:
+                    m_currentBand->_MenuItemHotTrack(VK_UP);
+                    break;
+                case VK_DOWN:
+                    m_currentBand->_MenuItemHotTrack(VK_DOWN);
+                    break;
+                }
+                break;
+            case WM_CHAR:
+                //if (msg->wParam >= 'a' && msg->wParam <= 'z')
+                //{
+                //    callNext = FALSE;
+                //    PostMessage(m_currentFocus, WM_SYSCHAR, wParam, lParam);
+                //}
+                break;
+            }
+
+            if (!callNext)
+                return 0;
+        }
+
+        return CallNextHookEx(m_hHook, nCode, wParam, lParam);
+    }
+
+    HRESULT PlaceHooks(HWND window)
+    {
+        //SetCapture(window);
+        m_hHook = SetWindowsHookEx(WH_GETMESSAGE, s_GetMsgHook, NULL, m_threadId);
+        return S_OK;
+    }
+
+    HRESULT RemoveHooks(HWND window)
+    {
+        UnhookWindowsHookEx(m_hHook);
+        //ReleaseCapture();
+        return S_OK;
+    }
+
+    HRESULT UpdateFocus(CMenuBand * newBand)
+    {
+        HRESULT hr;
+        HWND newFocus;
+
+        if (newBand == NULL)
+        {
+            hr = RemoveHooks(m_currentFocus);
+            m_currentFocus = NULL;
+            m_currentBand = NULL;
+            return S_OK;
+        }
+
+        hr = newBand->_GetTopLevelWindow(&newFocus);
+        if (FAILED(hr))
+            return hr;
+
+        if (!m_currentBand)
+        {
+            hr = PlaceHooks(newFocus);
+            if (FAILED(hr))
+                return hr;
+        }
+
+        m_currentFocus = newFocus;
+        m_currentBand = newBand;
+
+        return S_OK;
+    }
+
+public:
+    HRESULT PushMenu(CMenuBand * mb)
+    {
+        HRESULT hr;
+
+        hr = PushToArray(mb);
+        if (FAILED(hr))
+            return hr;
+
+        return UpdateFocus(mb);
+    }
+
+    HRESULT PopMenu(CMenuBand * mb)
+    {
+        CMenuBand * mbc;
+        HRESULT hr;
+
+        hr = PopFromArray(&mbc);
+        if (FAILED(hr))
+            return hr;
+
+        if (mb != mbc)
+            return E_FAIL;
+
+        hr = PeekArray(&mbc);
+
+        return UpdateFocus(mbc);
+    }
 };
 
+DWORD CMenuFocusManager::TlsIndex = 0;
+
 extern "C"
 HRESULT CMenuBand_Constructor(REFIID riid, LPVOID *ppv)
 {
@@ -271,622 +576,451 @@ 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)
+CMenuToolbarBase::CMenuToolbarBase(CMenuBand *menuBand, BOOL usePager) :
+    m_hwnd(NULL),
+    m_menuBand(menuBand),
+    m_hwndToolbar(NULL),
+    m_dwMenuFlags(0),
+    m_hasIdealSize(FALSE)
 {
-    WrapLogEnter("CMenuBand<%p>::SetToolbar()\n", this);
-    HRESULT hr = m_IShellMenu2->SetToolbar();
-    WrapLogExit("CMenuBand::SetToolbar() = %08x\n", hr);
-    return hr;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::SetMinWidth(THIS)
+HRESULT CMenuToolbarBase::IsWindowOwner(HWND hwnd)
 {
-    WrapLogEnter("CMenuBand<%p>::SetMinWidth()\n", this);
-    HRESULT hr = m_IShellMenu2->SetMinWidth();
-    WrapLogExit("CMenuBand::SetMinWidth() = %08x\n", hr);
-    return hr;
+    return (m_hwnd && m_hwnd == hwnd) ||
+           (m_hwndToolbar && m_hwndToolbar == hwnd) ? S_OK : S_FALSE;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::SetNoBorder(THIS)
+void CMenuToolbarBase::InvalidateDraw()
 {
-    WrapLogEnter("CMenuBand<%p>::SetNoBorder()\n", this);
-    HRESULT hr = m_IShellMenu2->SetNoBorder();
-    WrapLogExit("CMenuBand::SetNoBorder() = %08x\n", hr);
-    return hr;
+    InvalidateRect(m_hwnd, NULL, FALSE);
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::SetTheme(THIS)
+HRESULT CMenuToolbarBase::ShowWindow(BOOL fShow)
 {
-    WrapLogEnter("CMenuBand<%p>::SetTheme()\n", this);
-    HRESULT hr = m_IShellMenu2->SetTheme();
-    WrapLogExit("CMenuBand::SetTheme() = %08x\n", hr);
-    return hr;
-}
+    ::ShowWindow(m_hwnd, fShow ? SW_SHOW : SW_HIDE);
 
+    HIMAGELIST ilBig, ilSmall;
+    Shell_GetImageLists(&ilBig, &ilSmall);
 
-// *** IShellMenuAcc methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::GetTop(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::GetTop()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetTop();
-    WrapLogExit("CMenuBand::GetTop() = %08x\n", hr);
-    return hr;
-}
+    if (m_menuBand->UseBigIcons())
+    {
+        SendMessageW(m_hwndToolbar, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(ilBig));
+    }
+    else
+    {
+        SendMessageW(m_hwndToolbar, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(ilSmall));
+    }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetBottom(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::GetTracked(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_hwndToolbar);
+    if (m_hwndToolbar != m_hwnd)
+        DestroyWindow(m_hwnd);
+    m_hwndToolbar = NULL;
+    m_hwnd = NULL;
+    return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetParentSite(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::GetState(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;
+    }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::DoDefaultAction(THIS)
-{
-    WrapLogEnter("CMenuBand<%p>::GetBottom()\n", this);
-    HRESULT hr = m_IShellMenuAcc->GetBottom();
-    WrapLogExit("CMenuBand::GetBottom() = %08x\n", hr);
-    return hr;
-}
+    RECT rc;
 
-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 (!::GetClientRect(hwndParent, &rc) || (rc.left == rc.right) || (rc.top == rc.bottom))
+    {
+        rc.left = 0;
+        rc.top = 0;
+        rc.right = 1;
+        rc.bottom = 1;
+    }
 
-// *** 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;
-}
+    HWND hwndToolbar = CreateWindowEx(
+        tbExStyles, TOOLBARCLASSNAMEW, NULL,
+        tbStyles, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
+        hwndParent, NULL, _AtlBaseModule.GetModuleInstance(), 0);
 
-// *** 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 (hwndToolbar == NULL)
+        return E_FAIL;
 
-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;
-}
+    if (m_usePager)
+    {
+        LONG pgStyles = PGS_VERT | WS_CHILD | WS_VISIBLE;
+        LONG pgExStyles = 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;
-}
+        HWND hwndPager = CreateWindowEx(
+            pgExStyles, WC_PAGESCROLLER, NULL,
+            pgStyles, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
+            hwndParent, NULL, _AtlBaseModule.GetModuleInstance(), 0);
 
-// *** 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, hwndPager);
+        ::SetParent(hwndPager, 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;
-}
+        SendMessage(hwndPager, PGM_SETCHILD, 0, reinterpret_cast<LPARAM>(hwndToolbar));
+        m_hwndToolbar = hwndToolbar;
+        m_hwnd = hwndPager;
+    }
+    else
+    {
+        ::SetParent(hwndToolbar, hwndParent);
+        m_hwndToolbar = hwndToolbar;
+        m_hwnd = hwndToolbar;
+    }
+    
+    /* Identify the version of the used Common Controls DLL by sending the size of the TBBUTTON structure */
+    SendMessageW(hwndToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
 
-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;
-}
+    //if (dwFlags & SMINIT_TOPLEVEL)
+    //{
+    //    /* Hide the placeholders for the button images */
+    //    SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, 0);
+    //}
+    //else
+    int shiml;
+    if (m_menuBand->UseBigIcons())
+    {
+        shiml = SHIL_LARGE;
+        SendMessageW(hwndToolbar, TB_SETPADDING, 0, MAKELPARAM(0, 0));
+    }
+    else
+    {
+        shiml = SHIL_SMALL;
+    }
 
-// *** 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;
-}
+    IImageList * piml;
+    HRESULT hr = SHGetImageList(shiml, IID_PPV_ARG(IImageList, &piml));
+    if (SUCCEEDED(hr))
+    {
+        SendMessageW(hwndToolbar, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(piml));
+    }
+    else
+    {
+        SendMessageW(hwndToolbar, TB_SETIMAGELIST, 0, 0);
+    }
 
-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;
-}
+    SetWindowLongPtr(hwndToolbar, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
+    m_SubclassOld = (WNDPROC) SetWindowLongPtr(hwndToolbar, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(CMenuToolbarBase::s_SubclassProc));
 
-// *** 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;
+    return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::HasFocusIO()
+HRESULT CMenuToolbarBase::GetIdealSize(SIZE& size)
 {
-    WrapLogEnter("CMenuBand<%p>::HasFocusIO()\n", this);
-    HRESULT hr = m_IInputObject->HasFocusIO();
-    WrapLogExit("CMenuBand::HasFocusIO() = %08x\n", hr);
-    return hr;
-}
+    size.cx = size.cy = 0;
 
-HRESULT STDMETHODCALLTYPE CMenuBand::TranslateAcceleratorIO(LPMSG lpMsg)
-{
-    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 (m_hwndToolbar && !m_hasIdealSize)
+    {
+        SendMessageW(m_hwndToolbar, TB_AUTOSIZE, 0, 0);
+        SendMessageW(m_hwndToolbar, TB_GETMAXSIZE, 0, reinterpret_cast<LPARAM>(&m_idealSize));
+        m_hasIdealSize = TRUE;
+    }
 
-// *** 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;
+    size = m_idealSize;
+
+    return S_OK;
 }
-HRESULT STDMETHODCALLTYPE CMenuBand::GetSizeMax(ULARGE_INTEGER *pcbSize)
+
+HRESULT CMenuToolbarBase::SetPosSize(int x, int y, int cx, int cy)
 {
-    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;
+    if (m_hwnd != m_hwndToolbar)
+    {
+        SetWindowPos(m_hwndToolbar, NULL, x, y, cx, m_idealSize.cy, 0);
+    }
+    SetWindowPos(m_hwnd, NULL, x, y, cx, cy, 0);
+    DWORD btnSize = SendMessage(m_hwndToolbar, TB_GETBUTTONSIZE, 0, 0);
+    SendMessage(m_hwndToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(cx, HIWORD(btnSize)));
+    return S_OK;
 }
 
-// *** IPersist methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::GetClassID(CLSID *pClassID)
+HRESULT CMenuToolbarBase::GetWindow(HWND *phwnd)
 {
-    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;
+    if (!phwnd)
+        return E_FAIL;
+
+    *phwnd = m_hwnd;
+
+    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;
+            KillTimer(hWnd, TIMERID_HOTTRACK);
+
+            m_menuBand->_OnPopupSubMenu(-1, NULL, NULL, NULL);
+
+            if (HasSubMenu(m_hotItem) == S_OK)
+            {
+                PopupItem(m_hotItem);
+            }
         }
     }
 
-
-    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_hwndToolbar, TIMERID_HOTTRACK);
+        m_hotItem = -1;
+        m_menuBand->_OnHotItemChanged(NULL, -1);
+        m_menuBand->_MenuItemHotTrack(MPOS_CHILDTRACKING);
     }
-    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;
-}
-
+        DWORD elapsed = 0;
+        SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &elapsed, 0);
+        SetTimer(m_hwndToolbar, TIMERID_HOTTRACK, elapsed, NULL);
 
-// *** 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;
+        m_hotItem = hot->idNew;
+        m_menuBand->_OnHotItemChanged(this, m_hotItem);
+        m_menuBand->_MenuItemHotTrack(MPOS_CHILDTRACKING);
+    }
+    return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::OnSelect(DWORD dwSelectType)
+HRESULT CMenuToolbarBase::PopupSubMenu(UINT itemId, UINT index, IShellMenu* childShellMenu)
 {
-    WrapLogEnter("CMenuBand<%p>::OnSelect(DWORD dwSelectType=%08x)\n", this, dwSelectType);
-    HRESULT hr = m_IMenuPopup->OnSelect(dwSelectType);
-    WrapLogExit("CMenuBand::OnSelect() = %08x\n", hr);
-    return hr;
-}
+    IBandSite* pBandSite;
+    IDeskBar* pDeskBar;
 
-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;
-}
+    HRESULT hr = 0;
+    RECT rc = { 0 };
 
+    if (!SendMessage(m_hwndToolbar, TB_GETITEMRECT, index, reinterpret_cast<LPARAM>(&rc)))
+        return E_FAIL;
 
-// *** 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;
-}
+    POINT a = { rc.left, rc.top };
+    POINT b = { rc.right, rc.bottom };
 
-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;
-}
+    ClientToScreen(m_hwndToolbar, &a);
+    ClientToScreen(m_hwndToolbar, &b);
 
-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;
-}
+    POINTL pt = { b.x, a.y };
+    RECTL rcl = { a.x, a.y, b.x, b.y }; // maybe-TODO: fetch client area of deskbar?
 
 
-// *** 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;
-}
+#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
 
-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;
-}
+#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
 
-// *** IShellMenu methods ***
-HRESULT STDMETHODCALLTYPE CMenuBand::Initialize(IShellMenuCallback *psmc, UINT uId, UINT uIdAncestor, DWORD dwFlags)
-{
-    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;
-}
+    hr = pDeskBar->SetClient(pBandSite);
+    if (FAILED(hr))
+        return hr;
 
-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;
-}
+    hr = pBandSite->AddBand(childShellMenu);
+    if (FAILED(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;
-}
+    CComPtr<IMenuPopup> popup;
+    hr = pDeskBar->QueryInterface(IID_PPV_ARG(IMenuPopup, &popup));
+    if (FAILED(hr))
+        return hr;
 
-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;
-}
+    m_menuBand->_OnPopupSubMenu(itemId, popup, &pt, &rcl);
 
-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;
+    return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE CMenuBand::GetMenu(HMENU *phmenu, HWND *phwnd, DWORD *pdwFlags)
+HRESULT CMenuToolbarBase::PopupSubMenu(UINT index, HMENU menu)
 {
-    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;
-}
+    RECT rc = { 0 };
 
-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;
-}
+    if (!SendMessage(m_hwndToolbar, TB_GETITEMRECT, index, reinterpret_cast<LPARAM>(&rc)))
+        return E_FAIL;
 
-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;
-}
+    POINT b = { rc.right, rc.bottom };
 
-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;
-}
-#else
+    ClientToScreen(m_hwndToolbar, &b);
 
-#include <windowsx.h>
+    HMENU popup = GetSubMenu(menu, index);
 
-#define TBSTYLE_EX_VERTICAL 4
+    m_menuBand->_TrackSubMenuUsingTrackPopupMenu(popup, b.x, b.y);
 
-CMenuToolbarBase::CMenuToolbarBase(CMenuBand *menuBand) :
-    m_menuBand(menuBand),
-    m_hwnd(NULL),
-    m_dwMenuFlags(0)
-{
+    return S_OK;
 }
 
-HRESULT CMenuToolbarBase::ShowWindow(BOOL fShow)
+HRESULT CMenuToolbarBase::DoContextMenu(IContextMenu* contextMenu)
 {
-    ::ShowWindow(m_hwnd, fShow ? SW_SHOW : SW_HIDE);
+    HRESULT hr;
+    HMENU hPopup = CreatePopupMenu();
 
-    HIMAGELIST ilBig, ilSmall;
-    Shell_GetImageLists(&ilBig, &ilSmall);
+    if (hPopup == NULL)
+        return E_FAIL;
 
-    if (m_menuBand->UseBigIcons())
-    {
-        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, (LPARAM) ilBig);
-    }
-    else
+    hr = contextMenu->QueryContextMenu(hPopup, 0, 0, UINT_MAX, CMF_NORMAL);
+    if (FAILED(hr))
     {
-        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, (LPARAM) ilSmall);
+        DestroyMenu(hPopup);
+        return hr;
     }
 
-    return S_OK;
-}
+    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;
 
-HRESULT CMenuToolbarBase::Close()
-{
-    DestroyWindow(m_hwnd);
-    m_hwnd = NULL;
-    return S_OK;
+    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::CreateToolbar(HWND hwndParent, DWORD dwFlags)
+HRESULT CMenuToolbarBase::OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult)
 {
-    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 (dwFlags & SMINIT_VERTICAL)
+    theResult = 0;
+    if (HasSubMenu(wParam) == S_OK)
     {
-        tbStyles |= CCS_VERT;
-        tbExStyles |= TBSTYLE_EX_VERTICAL;
+        KillTimer(m_hwndToolbar, TIMERID_HOTTRACK);
+        PopupItem(wParam);
+        return S_FALSE;
     }
+    return m_menuBand->_MenuItemHotTrack(MPOS_EXECUTE);
+}
 
-    RECT rc;
+HRESULT CMenuToolbarBase::ChangeHotItem(DWORD dwSelectType)
+{
+    int prev = m_hotItem;
+    int index = -1;
 
-    if (!::GetClientRect(hwndParent, &rc) || (rc.left == rc.right) || (rc.top == rc.bottom))
+    if (dwSelectType != 0xFFFFFFFF)
     {
-        rc.left = 0;
-        rc.top = 0;
-        rc.right = 1;
-        rc.bottom = 1;
-    }
+        int count = SendMessage(m_hwndToolbar, TB_BUTTONCOUNT, 0, 0);
 
-    HWND hwndToolbar = CreateWindowEx(
-        tbExStyles, TOOLBARCLASSNAMEW, NULL,
-        tbStyles, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
-        hwndParent, NULL, _AtlBaseModule.GetModuleInstance(), 0);
-
-    if (hwndToolbar == NULL)
-        return E_FAIL;
-
-    ::SetParent(hwndToolbar, hwndParent);
+        if (m_hotItem >= 0)
+        {
+            TBBUTTONINFO info = { 0 };
+            info.cbSize = sizeof(TBBUTTONINFO);
+            info.dwMask = 0;
+            index = SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, m_hotItem, reinterpret_cast<LPARAM>(&info));
+        }
 
-    m_hwnd = hwndToolbar;
+        if (dwSelectType == VK_HOME)
+        {
+            index = 0;
+            dwSelectType = VK_DOWN;
+        }
+        else if (dwSelectType == VK_END)
+        {
+            index = count - 1;
+            dwSelectType = VK_UP;
+        }
+        else if (index < 0)
+        {
+            if (dwSelectType == VK_UP)
+            {
+                index = count - 1;
+            }
+            else if (dwSelectType == VK_DOWN)
+            {
+                index = 0;
+            }
+        }
+        else
+        {
+            if (dwSelectType == VK_UP)
+            {
+                index--;
+            }
+            else if (dwSelectType == VK_DOWN)
+            {
+                index++;
+            }
+        }
 
-    /* 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);
+        TBBUTTON btn = { 0 };
+        while (index >= 0 && index < count)
+        {
+            DWORD res = SendMessage(m_hwndToolbar, TB_GETBUTTON, index, reinterpret_cast<LPARAM>(&btn));
+            if (!res)
+                return E_FAIL;
 
-    HIMAGELIST ilBig, ilSmall;
-    Shell_GetImageLists(&ilBig, &ilSmall);
+            if (btn.dwData)
+            {
+                m_hotItem = btn.idCommand;
+                if (prev != m_hotItem)
+                {
+                    SendMessage(m_hwndToolbar, TB_SETHOTITEM, index, 0);
+                    return m_menuBand->_OnHotItemChanged(this, m_hotItem);
+                }
+                return S_OK;
+            }
 
-    //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);
+            if (dwSelectType == VK_UP)
+            {
+                index--;
+            }
+            else if (dwSelectType == VK_DOWN)
+            {
+                index++;
+            }
+        }
     }
-    else
+
+    m_hotItem = -1;
+    if (prev != m_hotItem)
     {
-        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, (LPARAM) ilSmall);
+        SendMessage(m_hwndToolbar, TB_SETHOTITEM, -1, 0);
+        m_menuBand->_OnHotItemChanged(NULL, -1);
     }
-
-    return S_OK;
-}
-
-HRESULT CMenuToolbarBase::GetWindow(HWND *phwnd)
-{
-    if (!phwnd)
-        return E_FAIL;
-
-    *phwnd = m_hwnd;
-
-    return S_OK;
+    return S_FALSE;
 }
 
 BOOL
@@ -909,7 +1043,7 @@ AllocAndGetMenuString(HMENU hMenu, UINT ItemIDByPosition, WCHAR** String)
 }
 
 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand *menuBand) :
-    CMenuToolbarBase(menuBand),
+    CMenuToolbarBase(menuBand, FALSE),
     m_hmenu(NULL)
 {
 }
@@ -940,14 +1074,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,32 +1094,102 @@ 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->_CallCBWithItemId(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;
+        SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb));
 
-        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, (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->_CallCBWithItemId(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;
+    hr = CMenuToolbarBase::OnCommand(wParam, lParam, theResult);
+    if (FAILED(hr))
+        return hr;
+
+    // in case the clicked item has a submenu, we do not need to execute the item
+    if (hr == S_FALSE)
+        return hr;
+
+    return m_menuBand->_CallCBWithItemId(wParam, SMC_EXEC, 0, 0);
+}
+
+HRESULT CMenuStaticToolbar::PopupItem(UINT uItem)
+{
+    TBBUTTONINFO info = { 0 };
+    info.cbSize = sizeof(TBBUTTONINFO);
+    info.dwMask = 0;
+    int index = SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, uItem, reinterpret_cast<LPARAM>(&info));
+    if (index < 0)
+        return E_FAIL;
+
+    TBBUTTON btn = { 0 };
+    SendMessage(m_hwndToolbar, 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->_CallCBWithItemId(uItem, SMC_GETOBJECT, reinterpret_cast<WPARAM>(&IID_IShellMenu), reinterpret_cast<LPARAM>(&shellMenu));
+        if (FAILED(hr))
+            return hr;
+
+        return PopupSubMenu(uItem, index, shellMenu);
+    }
+}
+
+HRESULT CMenuStaticToolbar::HasSubMenu(UINT uItem)
+{
+    TBBUTTONINFO info = { 0 };
+    info.cbSize = sizeof(TBBUTTONINFO);
+    info.dwMask = 0;
+    int index = SendMessage(m_hwndToolbar, 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),
+    CMenuToolbarBase(menuBand, TRUE),
     m_shellFolder(NULL)
 {
 }
@@ -997,25 +1201,27 @@ CMenuSFToolbar::~CMenuSFToolbar()
 HRESULT CMenuSFToolbar::FillToolbar()
 {
     HRESULT hr;
-    TBBUTTON tbb = { 0 };
     int i = 0;
     PWSTR MenuString;
 
-    tbb.fsState = TBSTATE_ENABLED;
-    tbb.fsStyle = 0;
-
     IEnumIDList * eidl;
-    m_shellFolder->EnumObjects(m_hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &eidl);
+    m_shellFolder->EnumObjects(m_hwndToolbar, 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;
-        SHCreateShellItem(NULL, m_shellFolder, item, &psi);
+        TBBUTTON tbb = { 0 };
+        tbb.fsState = TBSTATE_ENABLED;
+        tbb.fsStyle = 0;
+
+        CComPtr<IShellItem> psi;
+        hr = SHCreateShellItem(NULL, m_shellFolder, item, &psi);
+        if (FAILED(hr))
+            return hr;
 
         hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY, &MenuString);
         if (FAILED(hr))
@@ -1023,16 +1229,42 @@ HRESULT CMenuSFToolbar::FillToolbar()
 
         index = SHMapPIDLToSystemImageListIndex(m_shellFolder, item, &indexOpen);
 
-        tbb.idCommand = i++;
+        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_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb));
         HeapFree(GetProcessHeap(), 0, MenuString);
 
     }
     CoTaskMemFree(item);
 
+    // If no items were added, show the "empty" placeholder
+    if (i == 0)
+    {
+        TBBUTTON tbb = { 0 };
+        PCWSTR MenuString = L"(Empty)";
+
+        tbb.fsState = 0/*TBSTATE_DISABLED*/;
+        tbb.fsStyle = 0;
+        tbb.iString = (INT_PTR) MenuString;
+        tbb.iBitmap = -1;
+
+        SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb));
+
+        return S_OK;
+    }
+
     return hr;
 }
 
@@ -1076,28 +1308,148 @@ 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_hwndToolbar, TB_GETBUTTONINFO, uItem, reinterpret_cast<LPARAM>(&info));
+    if (index < 0)
+        return NULL;
+
+    if (pIndex)
+        *pIndex = index;
+
+    TBBUTTON btn = { 0 };
+    if (!SendMessage(m_hwndToolbar, 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_hwndToolbar, 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)
+{
+    HRESULT hr;
+    hr = CMenuToolbarBase::OnCommand(wParam, lParam, theResult);
+    if (FAILED(hr))
+        return hr;
+
+    // in case the clicked item has a submenu, we do not need to execute the item
+    if (hr == S_FALSE)
+        return hr;
+
+    return m_menuBand->_CallCBWithItemPidl(GetPidlFromId(wParam), 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 what 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(uItem, index, shellMenu);
+}
+
+HRESULT CMenuSFToolbar::HasSubMenu(UINT uItem)
+{
+    HRESULT hr;
+    CComPtr<IShellItem> psi;
+    hr = SHCreateShellItem(NULL, m_shellFolder, GetPidlFromId(uItem), &psi);
+    if (FAILED(hr))
+        return S_FALSE;
+
+    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),
     m_staticToolbar(NULL),
     m_SFToolbar(NULL),
-    m_useBigIcons(FALSE)
+    m_site(NULL),
+    m_psmc(NULL),
+    m_subMenuChild(NULL),
+    m_useBigIcons(FALSE),
+    m_hotBar(NULL),
+    m_hotItem(-1),
+    m_popupItem(-1)
 {
+    m_focusManager = CMenuFocusManager::AcquireManager();
+
+    m_marlett = CreateFont(
+        0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET,
+        OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
+        DEFAULT_QUALITY, FF_DONTCARE, L"Marlett");
 }
 
 CMenuBand::~CMenuBand()
 {
-    if (m_site)
-        m_site->Release();
-
-    if (m_psmc)
-        m_psmc->Release();
+    CMenuFocusManager::ReleaseManager(m_focusManager);
 
     if (m_staticToolbar)
         delete m_staticToolbar;
 
     if (m_SFToolbar)
         delete m_SFToolbar;
+
+    DeleteObject(m_marlett);
 }
 
 HRESULT STDMETHODCALLTYPE  CMenuBand::Initialize(
@@ -1106,19 +1458,15 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::Initialize(
     UINT uIdAncestor,
     DWORD dwFlags)
 {
-    if (m_psmc)
-        m_psmc->Release();
-
-    m_psmc = psmc;
+    if (m_psmc != psmc)
+        m_psmc = psmc;
     m_uId = uId;
     m_uIdAncestor = uIdAncestor;
     m_dwFlags = dwFlags;
 
     if (m_psmc)
     {
-        m_psmc->AddRef();
-
-        _CallCB(SMC_CREATE, 0, (LPARAM) &m_UserData);
+        _CallCB(SMC_CREATE, 0, reinterpret_cast<LPARAM>(&m_UserData));
     }
 
     return S_OK;
@@ -1157,27 +1505,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,44 +1542,58 @@ 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();
+    m_site = NULL;
 
     if (pUnkSite == NULL)
         return S_OK;
 
     hwndParent = NULL;
-    hResult = pUnkSite->QueryInterface(IID_PPV_ARG(IOleWindow, &m_site));
-    if (SUCCEEDED(hResult))
-    {
-        m_site->GetWindow(&hwndParent);
-        m_site->Release();
-    }
+    hr = pUnkSite->QueryInterface(IID_PPV_ARG(IOleWindow, &m_site));
+    if (FAILED(hr))
+        return hr;
+
+    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;
+    hr = IUnknown_QueryService(m_site, SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &m_subMenuParent));
+    if (FAILED(hr))
+        return hr;
+
+    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,134 +1618,73 @@ 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;
+    SIZE sizeStatic = { 0 };
+    SIZE sizeShlFld = { 0 };
+    HRESULT hr = S_OK;
 
     if (m_staticToolbar != NULL)
-        hResult = m_staticToolbar->GetWindow(&hwndStatic);
-    if (FAILED(hResult))
-        return hResult;
+        hr = m_staticToolbar->GetIdealSize(sizeStatic);
+    if (FAILED(hr))
+        return hr;
 
     if (m_SFToolbar != NULL)
-        hResult = m_SFToolbar->GetWindow(&hwndShlFld);
-    if (FAILED(hResult))
-        return hResult;
+        hr = m_SFToolbar->GetIdealSize(sizeShlFld);
+    if (FAILED(hr))
+        return hr;
 
-    if (hwndStatic == NULL && hwndShlFld == NULL)
+    if (m_staticToolbar == NULL && m_SFToolbar == 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);
+    int sy = min(prc->bottom - prc->top, sizeStatic.cy + sizeShlFld.cy);
 
-    int sy = max(prc->bottom - prc->top, sizeStaticY.cy + sizeShlFldY.cy);
+    int syStatic = sizeStatic.cy;
+    int syShlFld = sy - syStatic;
 
-    if (hwndShlFld)
+    if (m_SFToolbar)
     {
-        SetWindowPos(hwndShlFld, NULL,
-            prc->left,
-            prc->top,
-            prc->right - prc->left,
-            sizeShlFldY.cy,
-            0);
-        DWORD btnSize = SendMessage(hwndShlFld, TB_GETBUTTONSIZE, 0, 0);
-        SendMessage(hwndShlFld, TB_SETBUTTONSIZE, 0, MAKELPARAM(prc->right - prc->left, HIWORD(btnSize)));
+        m_SFToolbar->SetPosSize(
+            prc->left, 
+            prc->top, 
+            prc->right - prc->left, 
+            syShlFld);
     }
-    if (hwndStatic)
+    if (m_staticToolbar)
     {
-        SetWindowPos(hwndStatic, hwndShlFld,
-            prc->left,
-            prc->top + sizeShlFldY.cy,
-            prc->right - prc->left,
-            sy - sizeShlFldY.cy,
-            0);
-        DWORD btnSize = SendMessage(hwndStatic, TB_GETBUTTONSIZE, 0, 0);
-        SendMessage(hwndStatic, TB_SETBUTTONSIZE, 0, MAKELPARAM(prc->right - prc->left, HIWORD(btnSize)));
+        m_staticToolbar->SetPosSize(
+            prc->left, 
+            prc->top + syShlFld, 
+            prc->right - prc->left, 
+            syStatic);
     }
 
     return S_OK;
 }
-
 HRESULT STDMETHODCALLTYPE  CMenuBand::GetBandInfo(
     DWORD dwBandID,
     DWORD dwViewMode,
     DESKBANDINFO *pdbi)
 {
-    HWND hwndStatic = NULL;
-    HWND hwndShlFld = NULL;
-    HRESULT hResult = S_OK;
+    SIZE sizeStatic = { 0 };
+    SIZE sizeShlFld = { 0 };
+
+    HRESULT hr = S_OK;
 
     if (m_staticToolbar != NULL)
-        hResult = m_staticToolbar->GetWindow(&hwndStatic);
-    if (FAILED(hResult))
-        return hResult;
+        hr = m_staticToolbar->GetIdealSize(sizeStatic);
+    if (FAILED(hr))
+        return hr;
 
     if (m_SFToolbar != NULL)
-        hResult = m_SFToolbar->GetWindow(&hwndShlFld);
-    if (FAILED(hResult))
-        return hResult;
+        hr = m_SFToolbar->GetIdealSize(sizeShlFld);
+    if (FAILED(hr))
+        return hr;
 
-    if (hwndStatic == NULL && hwndShlFld == NULL)
+    if (m_staticToolbar == NULL && m_SFToolbar == NULL)
         return E_FAIL;
 
-    // HACK (?)
-    if (pdbi->dwMask == 0)
-    {
-        pdbi->dwMask = DBIM_MINSIZE | DBIM_MAXSIZE | DBIM_INTEGRAL | DBIM_ACTUAL | DBIM_TITLE | DBIM_MODEFLAGS | DBIM_BKCOLOR;
-    }
-
-    if (pdbi->dwMask & DBIM_MINSIZE)
-    {
-        SIZE sizeStatic = { 0 };
-        SIZE sizeShlFld = { 0 };
-
-        if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeStatic);
-        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeShlFld);
-
-        pdbi->ptMinSize.x = 0;
-        pdbi->ptMinSize.y = sizeStatic.cy + sizeShlFld.cy;
-    }
-    if (pdbi->dwMask & DBIM_MAXSIZE)
-    {
-        SIZE sizeStatic = { 0 };
-        SIZE sizeShlFld = { 0 };
-
-        if (hwndStatic) SendMessageW(hwndStatic, TB_GETMAXSIZE, 0, (LPARAM) &sizeStatic);
-        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETMAXSIZE, 0, (LPARAM) &sizeShlFld);
-
-        pdbi->ptMaxSize.x = sizeStatic.cx + sizeShlFld.cx; // ignored
-        pdbi->ptMaxSize.y = sizeStatic.cy + sizeShlFld.cy;
-    }
-    if (pdbi->dwMask & DBIM_INTEGRAL)
-    {
-        pdbi->ptIntegral.x = 0;
-        pdbi->ptIntegral.y = 0;
-    }
-    if (pdbi->dwMask & DBIM_ACTUAL)
-    {
-        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;
+    pdbi->ptMaxSize.x = max(sizeStatic.cx, sizeShlFld.cx) + 20;
+    pdbi->ptMaxSize.y = sizeStatic.cy + sizeShlFld.cy;
 
-        if (hwndStatic) SendMessageW(hwndStatic, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeStatic);
-        if (hwndShlFld) SendMessageW(hwndShlFld, TB_GETIDEALSIZE, TRUE, (LPARAM) &sizeShlFld);
-        pdbi->ptActual.y = sizeStatic.cy + sizeShlFld.cy;
-    }
-    if (pdbi->dwMask & DBIM_TITLE)
-        wcscpy(pdbi->wszTitle, L"");
-    if (pdbi->dwMask & DBIM_MODEFLAGS)
-        pdbi->dwModeFlags = DBIMF_UNDELETEABLE;
-    if (pdbi->dwMask & DBIM_BKCOLOR)
-        pdbi->crBkgnd = 0;
     return S_OK;
 }
 
@@ -1401,7 +1703,16 @@ HRESULT STDMETHODCALLTYPE  CMenuBand::ShowDW(BOOL fShow)
         return hr;
 
     if (fShow)
-        return _CallCB(SMC_INITMENU, 0, 0);
+    {
+        hr = _CallCB(SMC_INITMENU, 0, 0);
+        if (FAILED(hr))
+            return hr;
+    }
+
+    if (fShow)
+        hr = m_focusManager->PushMenu(this);
+    else
+        hr = m_focusManager->PopMenu(this);
 
     return S_OK;
 }
@@ -1432,8 +1743,29 @@ HRESULT STDMETHODCALLTYPE CMenuBand::ContextSensitiveHelp(BOOL fEnterMode)
 
 HRESULT STDMETHODCALLTYPE CMenuBand::UIActivateIO(BOOL fActivate, LPMSG lpMsg)
 {
-    UNIMPLEMENTED;
-    return S_OK;
+    HRESULT hr;
+
+    hr = m_subMenuParent->SetSubMenu(this, fActivate);
+    if (FAILED(hr))
+        return hr;
+
+    if (fActivate)
+    {
+        CComPtr<IOleWindow> pTopLevelWindow;
+        hr = IUnknown_QueryService(m_site, SID_SMenuPopup, IID_PPV_ARG(IOleWindow, &pTopLevelWindow));
+        if (FAILED(hr))
+            return hr;
+
+        hr = pTopLevelWindow->GetWindow(&m_topLevelWindow);
+        if (FAILED(hr))
+            return hr;
+    }
+    else
+    {
+        m_topLevelWindow = NULL;
+    }
+
+    return S_FALSE;
 }
 
 HRESULT STDMETHODCALLTYPE CMenuBand::HasFocusIO()
@@ -1493,7 +1825,7 @@ HRESULT STDMETHODCALLTYPE CMenuBand::Exec(const GUID *pguidCmdGroup, DWORD nCmdI
     {
         if (nCmdID == 16) // set (big) icon size
         {
-            this->m_useBigIcons = TRUE;
+            this->m_useBigIcons = nCmdexecopt == 2;
             return S_OK;
         }
         else if (nCmdID == 19) // popup-related
@@ -1508,7 +1840,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,8 +1856,32 @@ HRESULT STDMETHODCALLTYPE CMenuBand::Popup(POINTL *ppt, RECTL *prcExclude, MP_PO
 
 HRESULT STDMETHODCALLTYPE CMenuBand::OnSelect(DWORD dwSelectType)
 {
-    UNIMPLEMENTED;
-    return S_OK;
+    switch (dwSelectType)
+    {
+    case MPOS_CHILDTRACKING:
+        // TODO: Cancel timers?
+        return m_subMenuParent->OnSelect(dwSelectType);
+    case MPOS_SELECTLEFT:
+        if (m_subMenuChild)
+            m_subMenuChild->OnSelect(MPOS_CANCELLEVEL);
+        return m_subMenuParent->OnSelect(dwSelectType);
+    case MPOS_SELECTRIGHT:
+        if (m_hotBar && m_hotItem >= 0)
+        {
+            // TODO: popup the current child if it has subitems, otherwise spread up.
+        }
+        return m_subMenuParent->OnSelect(dwSelectType);
+    case MPOS_EXECUTE:
+    case MPOS_FULLCANCEL:
+        if (m_subMenuChild)
+            m_subMenuChild->OnSelect(dwSelectType);
+        return m_subMenuParent->OnSelect(dwSelectType);
+    case MPOS_CANCELLEVEL:
+        if (m_subMenuChild)
+            m_subMenuChild->OnSelect(dwSelectType);
+        break;
+    }
+    return S_FALSE;
 }
 
 HRESULT STDMETHODCALLTYPE CMenuBand::SetSubMenu(IMenuPopup *pmp, BOOL fSet)
@@ -1540,7 +1898,15 @@ HRESULT STDMETHODCALLTYPE CMenuBand::SetClient(IUnknown *punkClient)
 
 HRESULT STDMETHODCALLTYPE CMenuBand::GetClient(IUnknown **ppunkClient)
 {
-    UNIMPLEMENTED;
+    // HACK, so I can test for a submenu in the DeskBar
+    //UNIMPLEMENTED;
+    if (ppunkClient)
+    {
+        if (m_subMenuChild)
+            *ppunkClient = m_subMenuChild;
+        else
+            *ppunkClient = NULL;
+    }
     return S_OK;
 }
 
@@ -1565,26 +1931,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)
@@ -1614,15 +1980,117 @@ HRESULT STDMETHODCALLTYPE CMenuBand::SetMenuToolbar(IUnknown *punk, DWORD dwFlag
 
 HRESULT STDMETHODCALLTYPE CMenuBand::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *theResult)
 {
+    RECT rc;
+    HDC hdc;
+    HBRUSH bgBrush;
+    HBRUSH hotBrush;
+
     *theResult = 0;
-    if (uMsg == WM_NOTIFY)
+    switch (uMsg)
     {
-        NMHDR * hdr = (LPNMHDR) lParam;
+    case WM_COMMAND:
+
+        if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd) == S_OK)
+        {
+            return m_staticToolbar->OnCommand(wParam, lParam, theResult);
+        }
+
+        if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd) == S_OK)
+        {
+            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;
+        NMPGCALCSIZE* csize;
+        TBBUTTONINFO btni;
         switch (hdr->code)
         {
+        case PGN_CALCSIZE:
+            csize = reinterpret_cast<LPNMPGCALCSIZE>(hdr);
+
+            if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd) == S_OK)
+            {
+                SIZE tbs;
+                m_staticToolbar->GetIdealSize(tbs);
+                if (csize->dwFlag == PGF_CALCHEIGHT)
+                {
+                    csize->iHeight = tbs.cy;
+                }
+                else if (csize->dwFlag == PGF_CALCWIDTH)
+                {
+                    csize->iHeight = tbs.cx;
+                }
+            }
+
+            if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd) == S_OK)
+            {
+                SIZE tbs;
+                m_SFToolbar->GetIdealSize(tbs);
+                if (csize->dwFlag == PGF_CALCHEIGHT)
+                {
+                    csize->iHeight = tbs.cy;
+                }
+                else if (csize->dwFlag == PGF_CALCWIDTH)
+                {
+                    csize->iHeight = tbs.cx;
+                }
+            }
+            return S_OK;
+
+        case TBN_DROPDOWN:
+
+            if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd) == S_OK)
+            {
+                WPARAM wp = reinterpret_cast<LPNMTOOLBAR>(hdr)->iItem;
+                return m_staticToolbar->OnCommand(wp, 0, theResult);
+            }
+
+            if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd) == S_OK)
+            {
+                WPARAM wp = reinterpret_cast<LPNMTOOLBAR>(hdr)->iItem;
+                return m_SFToolbar->OnCommand(wp, 0, theResult);
+            }
+
+            return S_OK;
+
+        case TBN_HOTITEMCHANGE:
+            hot = reinterpret_cast<LPNMTBHOTITEM>(hdr);
+
+            if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd) == S_OK)
+            {
+                return m_staticToolbar->OnHotItemChange(hot);
+            }
+
+            if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd) == S_OK)
+            {
+                return m_SFToolbar->OnHotItemChange(hot);
+            }
+
+            return S_OK;
+
+        case NM_RCLICK:
+            rclick = reinterpret_cast<LPNMMOUSE>(hdr);
+
+            if (m_staticToolbar && m_staticToolbar->IsWindowOwner(hWnd) == S_OK)
+            {
+                return m_staticToolbar->OnContextMenu(rclick);
+            }
+
+            if (m_SFToolbar && m_SFToolbar->IsWindowOwner(hWnd) == S_OK)
+            {
+                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:
@@ -1638,11 +2106,18 @@ HRESULT STDMETHODCALLTYPE CMenuBand::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wPa
                 cdraw->clrTextHighlight = GetSysColor(COLOR_HIGHLIGHTTEXT);
                 cdraw->clrHighlightHotTrack = GetSysColor(COLOR_HIGHLIGHTTEXT);
 
-                RECT rc = cdraw->nmcd.rc;
-                HDC hdc = cdraw->nmcd.hdc;
+                bgBrush = GetSysColorBrush(COLOR_MENU);
+                hotBrush = GetSysColorBrush(COLOR_MENUHILIGHT);
+
+                rc = cdraw->nmcd.rc;
+                hdc = cdraw->nmcd.hdc;
 
-                HBRUSH bgBrush = GetSysColorBrush(COLOR_MENU);
-                HBRUSH hotBrush = GetSysColorBrush(COLOR_MENUHILIGHT);
+                if (cdraw->nmcd.uItemState != CDIS_DISABLED &&
+                    ((INT)cdraw->nmcd.dwItemSpec == m_hotItem ||
+                    (m_hotItem < 0 && (INT)cdraw->nmcd.dwItemSpec == m_popupItem)))
+                {
+                    cdraw->nmcd.uItemState = CDIS_HOT;
+                }
 
                 switch (cdraw->nmcd.uItemState)
                 {
@@ -1655,37 +2130,37 @@ HRESULT STDMETHODCALLTYPE CMenuBand::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wPa
                     break;
                 }
 
-                *theResult = TBCDRF_NOBACKGROUND | TBCDRF_NOEDGES | TBCDRF_NOETCHEDEFFECT | TBCDRF_HILITEHOTTRACK;
+                *theResult = CDRF_NOTIFYPOSTPAINT | TBCDRF_NOBACKGROUND | TBCDRF_NOEDGES | TBCDRF_NOOFFSET | TBCDRF_NOMARK | 0x00800000; // FIXME: the last bit is Vista+, for debugging only
+                return S_OK;
+
+            case CDDS_ITEMPOSTPAINT:
+                btni.cbSize = sizeof(btni);
+                btni.dwMask = TBIF_STYLE;
+                SendMessage(hWnd, TB_GETBUTTONINFO, cdraw->nmcd.dwItemSpec, reinterpret_cast<LPARAM>(&btni));
+                if (btni.fsStyle & BTNS_DROPDOWN)
+                {
+                    SelectObject(cdraw->nmcd.hdc, m_marlett);
+                    WCHAR text [] = L"8";
+                    SetBkMode(cdraw->nmcd.hdc, TRANSPARENT);
+                    DrawTextEx(cdraw->nmcd.hdc, text, 1, &cdraw->nmcd.rc, DT_NOCLIP | DT_VCENTER | DT_RIGHT | DT_SINGLELINE, NULL);
+                }
+                *theResult = TRUE;
                 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) == S_OK)
         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) == S_OK)
         return S_OK;
 
     return S_FALSE;
@@ -1763,7 +2238,17 @@ HRESULT STDMETHODCALLTYPE CMenuBand::IsEmpty(THIS)
     return S_OK;
 }
 
-HRESULT CMenuBand::CallCBWithId(UINT Id, UINT uMsg, WPARAM wParam, LPARAM lParam)
+HRESULT CMenuBand::_CallCBWithItemId(UINT id, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    return _CallCB(uMsg, wParam, lParam, id);
+}
+
+HRESULT CMenuBand::_CallCBWithItemPidl(LPITEMIDLIST pidl, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    return _CallCB(uMsg, wParam, lParam, 0, pidl);
+}
+
+HRESULT CMenuBand::_CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam, UINT id, LPITEMIDLIST pidl)
 {
     if (!m_psmc)
         return S_FALSE;
@@ -1772,11 +2257,12 @@ HRESULT CMenuBand::CallCBWithId(UINT Id, UINT uMsg, WPARAM wParam, LPARAM lParam
     GetWindow(&hwnd);
 
     SMDATA smData = { 0 };
-    smData.punk = (IShellMenu2*)this;
-    smData.uId = Id;
+    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;
@@ -1791,31 +2277,110 @@ HRESULT CMenuBand::CallCBWithId(UINT Id, UINT uMsg, WPARAM wParam, LPARAM lParam
     return hr;
 }
 
-HRESULT CMenuBand::_CallCB(UINT uMsg, WPARAM wParam, LPARAM lParam)
+HRESULT CMenuBand::_TrackSubMenuUsingTrackPopupMenu(HMENU popup, INT x, INT y)
 {
-    if (!m_psmc)
-        return S_FALSE;
+    ::TrackPopupMenu(popup, 0, x, y, 0, m_menuOwner, NULL);
+    return S_OK;
+}
 
-    HWND hwnd;
-    GetWindow(&hwnd);
+HRESULT CMenuBand::_GetTopLevelWindow(HWND*topLevel)
+{
+    *topLevel = m_topLevelWindow;
+    return S_OK;
+}
 
-    SMDATA smData = { 0 };
-    smData.punk = (IShellMenu2*)this;
-    smData.uIdParent = m_uId;
-    smData.uIdAncestor = m_uIdAncestor;
-    smData.hwnd = hwnd;
-    if (m_staticToolbar)
+HRESULT CMenuBand::_OnHotItemChanged(CMenuToolbarBase * tb, INT id)
+{
+    if (m_hotBar && m_hotBar != tb)
+        m_hotBar->ChangeHotItem(-1);
+    m_hotBar = tb;
+    m_hotItem = id;
+    if (m_staticToolbar) m_staticToolbar->InvalidateDraw();
+    if (m_SFToolbar) m_SFToolbar->InvalidateDraw();
+    return S_OK;
+}
+
+HRESULT CMenuBand::_MenuItemHotTrack(DWORD changeType)
+{
+    HRESULT hr;
+
+    if (changeType == VK_DOWN)
     {
-        smData.hmenu = m_hmenu;
+        if (m_SFToolbar && (m_hotBar == m_SFToolbar || m_hotBar == NULL))
+        {
+            hr = m_SFToolbar->ChangeHotItem(VK_DOWN);
+            if (hr == S_FALSE)
+            {
+                if (m_staticToolbar)
+                    return m_staticToolbar->ChangeHotItem(VK_HOME);
+                else
+                    return m_SFToolbar->ChangeHotItem(VK_HOME);
+            }
+            return hr;
+        }
+        else if (m_staticToolbar && m_hotBar == m_staticToolbar)
+        {
+            hr = m_staticToolbar->ChangeHotItem(VK_DOWN);
+            if (hr == S_FALSE)
+            {
+                if (m_SFToolbar)
+                    return m_SFToolbar->ChangeHotItem(VK_HOME);
+                else
+                    return m_staticToolbar->ChangeHotItem(VK_HOME);
+            }
+            return hr;
+        }
     }
-    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;
+    else if (changeType == VK_UP)
+    {
+        if (m_staticToolbar && (m_hotBar == m_staticToolbar || m_hotBar == NULL))
+        {
+            hr = m_staticToolbar->ChangeHotItem(VK_DOWN);
+            if (hr == S_FALSE)
+            {
+                if (m_SFToolbar)
+                    return m_SFToolbar->ChangeHotItem(VK_END);
+                else
+                    return m_staticToolbar->ChangeHotItem(VK_END);
+            }
+            return hr;
+        }
+        else if (m_SFToolbar && m_hotBar == m_SFToolbar)
+        {
+            hr = m_SFToolbar->ChangeHotItem(VK_UP);
+            if (hr == S_FALSE)
+            {
+                if (m_staticToolbar)
+                    return m_staticToolbar->ChangeHotItem(VK_END);
+                else
+                    return m_SFToolbar->ChangeHotItem(VK_END);
+            }
+            return hr;
+        }
+    }
+    else
+    {
+        m_subMenuParent->OnSelect(changeType);
+    }
+    return S_OK;
 }
 
-#endif
\ No newline at end of file
+HRESULT CMenuBand::_OnPopupSubMenu(INT popupItem, IMenuPopup * popup, POINTL * pAt, RECTL * pExclude)
+{
+    if (m_subMenuChild)
+    {
+        HRESULT hr = m_subMenuChild->OnSelect(MPOS_CANCELLEVEL);
+        if (FAILED(hr))
+            return hr;
+    }
+    m_popupItem = popupItem;
+    m_subMenuChild = popup;
+    if (popup)
+    {
+        IUnknown_SetSite(popup, m_subMenuParent);
+        popup->Popup(pAt, pExclude, MPPF_RIGHT);
+    }
+    if (m_staticToolbar) m_staticToolbar->InvalidateDraw();
+    if (m_SFToolbar) m_SFToolbar->InvalidateDraw();
+    return S_OK;
+}