[RSHELL]
[reactos.git] / base / shell / rshell / CMenuToolbars.cpp
index da1b663..aa879a2 100644 (file)
@@ -19,8 +19,9 @@
  */
 #include "precomp.h"
 #include <windowsx.h>
-#include <CommonControls.h>
+#include <commoncontrols.h>
 #include <shlwapi_undoc.h>
+#include <uxtheme.h>
 
 #include "CMenuBand.h"
 #include "CMenuToolbars.h"
@@ -34,25 +35,282 @@ HRESULT WINAPI SHGetImageList(
     _Out_  void **ppv
     );
 
-#define TBSTYLE_EX_VERTICAL 4
-
+// FIXME: Enable if/when wine comctl supports this flag properly
+#define USE_TBSTYLE_EX_VERTICAL 0
 
+// User-defined timer ID used while hot-tracking around the menu
 #define TIMERID_HOTTRACK 1
-#define SUBCLASS_ID_MENUBAND 1
+
+HRESULT CMenuToolbarBase::OnWinEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *theResult)
+{
+    NMHDR * hdr;
+
+    *theResult = 0;
+    switch (uMsg)
+    {
+    case WM_COMMAND:
+        //return OnCommand(wParam, lParam, theResult);
+        return S_OK;
+
+    case WM_NOTIFY:
+        hdr = reinterpret_cast<LPNMHDR>(lParam);
+        switch (hdr->code)
+        {
+        case TBN_DELETINGBUTTON:
+            return OnDeletingButton(reinterpret_cast<LPNMTOOLBAR>(hdr));
+
+        case PGN_CALCSIZE:
+            return OnPagerCalcSize(reinterpret_cast<LPNMPGCALCSIZE>(hdr));
+
+        case TBN_DROPDOWN:
+            return ProcessClick(reinterpret_cast<LPNMTOOLBAR>(hdr)->iItem);
+
+        case TBN_HOTITEMCHANGE:
+            //return OnHotItemChange(reinterpret_cast<LPNMTBHOTITEM>(hdr), theResult);
+            return S_OK;
+
+        case NM_RCLICK:
+            return OnContextMenu(reinterpret_cast<LPNMMOUSE>(hdr));
+
+        case NM_CUSTOMDRAW:
+            return OnCustomDraw(reinterpret_cast<LPNMTBCUSTOMDRAW>(hdr), theResult);
+
+        case TBN_GETINFOTIP:
+            return OnGetInfoTip(reinterpret_cast<LPNMTBGETINFOTIP>(hdr));
+
+            // Silence unhandled items so that they don't print as unknown
+        case RBN_CHILDSIZE:
+            return S_OK;
+
+        case TTN_GETDISPINFO:
+            return S_OK;
+
+        case NM_RELEASEDCAPTURE:
+            break;
+
+        case NM_CLICK:
+        case NM_RDOWN:
+        case NM_LDOWN:
+            break;
+
+        case TBN_GETDISPINFO:
+            break;
+
+        case TBN_BEGINDRAG:
+        case TBN_ENDDRAG:
+            break;
+
+        case NM_TOOLTIPSCREATED:
+            break;
+
+            // Unknown
+        case -714: return S_FALSE;
+
+        default:
+            TRACE("WM_NOTIFY unknown code %d, %d\n", hdr->code, hdr->idFrom);
+            return S_OK;
+        }
+        return S_FALSE;
+    }
+
+    return S_FALSE;
+}
+
+LRESULT CALLBACK CMenuToolbarBase::s_SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    CMenuToolbarBase * pthis = reinterpret_cast<CMenuToolbarBase *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
+    return pthis->SubclassProc(hWnd, uMsg, wParam, lParam);
+}
+
+LRESULT CMenuToolbarBase::SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    LRESULT lr;
+
+    switch (uMsg)
+    {
+    case WM_USER_ISTRACKEDITEM:
+        m_SubclassOld(hWnd, uMsg, wParam, lParam);
+        return IsTrackedItem(wParam);
+    case WM_USER_CHANGETRACKEDITEM:
+        m_SubclassOld(hWnd, uMsg, wParam, lParam);
+        return ChangeTrackedItem(wParam, LOWORD(lParam), HIWORD(lParam));
+
+    case WM_COMMAND:
+        OnWinEvent(hWnd, uMsg, wParam, lParam, &lr);
+        break;
+    case WM_NOTIFY:
+        OnWinEvent(hWnd, uMsg, wParam, lParam, &lr);
+        break;
+    case WM_TIMER:
+        OnPopupTimer(wParam);
+    }
+
+    return m_SubclassOld(hWnd, uMsg, wParam, lParam);
+}
+
+HRESULT CMenuToolbarBase::DisableMouseTrack(BOOL bDisable)
+{
+    if (m_disableMouseTrack != bDisable)
+    {
+        m_disableMouseTrack = bDisable;
+        TRACE("DisableMouseTrack %d\n", bDisable);
+    }
+    return S_OK;
+}
+
+HRESULT CMenuToolbarBase::OnPagerCalcSize(LPNMPGCALCSIZE csize)
+{
+    SIZE tbs;
+    GetSizes(NULL, &tbs, NULL);
+    if (csize->dwFlag == PGF_CALCHEIGHT)
+    {
+        csize->iHeight = tbs.cy;
+    }
+    else if (csize->dwFlag == PGF_CALCWIDTH)
+    {
+        csize->iWidth = tbs.cx;
+    }
+    return S_OK;
+}
+
+HRESULT CMenuToolbarBase::OnCustomDraw(LPNMTBCUSTOMDRAW cdraw, LRESULT * theResult)
+{
+    RECT     rc;
+    HDC      hdc;
+    COLORREF clrText;
+    HBRUSH   bgBrush;
+    bool     isHot, isPopup;
+    TBBUTTONINFO btni;
+
+    switch (cdraw->nmcd.dwDrawStage)
+    {
+    case CDDS_PREPAINT:
+        *theResult = CDRF_NOTIFYITEMDRAW;
+        return S_OK;
+
+    case CDDS_ITEMPREPAINT:
+
+        rc = cdraw->nmcd.rc;
+        hdc = cdraw->nmcd.hdc;
+
+        // The item with an active submenu gets the CHECKED flag.
+        isHot = m_hotBar == this && (int) cdraw->nmcd.dwItemSpec == m_hotItem;
+        isPopup = m_popupBar == this && (int) cdraw->nmcd.dwItemSpec == m_popupItem;
+
+        if (m_initFlags & SMINIT_VERTICAL || IsAppThemed())
+        {
+            // Remove HOT and CHECKED flags (will restore HOT if necessary)
+            cdraw->nmcd.uItemState &= ~(CDIS_HOT | CDIS_CHECKED);
+
+            // Decide on the colors
+            if (isHot || (m_hotItem < 0 && isPopup))
+            {
+                cdraw->nmcd.uItemState |= CDIS_HOT;
+
+                clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
+                bgBrush = GetSysColorBrush(m_useFlatMenus ? COLOR_MENUHILIGHT : COLOR_HIGHLIGHT);
+            }
+            else
+            {
+                clrText = GetSysColor(COLOR_MENUTEXT);
+                bgBrush = GetSysColorBrush(COLOR_MENU);
+            }
+
+            // Paint the background color with the selected color
+            FillRect(hdc, &rc, bgBrush);
+
+            // Set the text color in advance, this color will be assigned when the ITEMPOSTPAINT triggers
+            SetTextColor(hdc, clrText);
+
+            // Set the text color, will be used by the internal drawing code
+            cdraw->clrText = clrText;
+            cdraw->iListGap += 4;
+
+            // Tell the default drawing code we don't want any fanciness, not even a background.
+            *theResult = CDRF_NOTIFYPOSTPAINT | TBCDRF_NOBACKGROUND | TBCDRF_NOEDGES | TBCDRF_NOOFFSET | TBCDRF_NOMARK | 0x00800000; // FIXME: the last bit is Vista+, useful for debugging only
+        }
+        else
+        {
+            // Remove HOT and CHECKED flags (will restore HOT if necessary)
+            cdraw->nmcd.uItemState &= ~CDIS_HOT;
+
+            // Decide on the colors
+            if (isHot || (m_hotItem < 0 && isPopup))
+            {
+                cdraw->nmcd.uItemState |= CDIS_HOT;
+            }
+
+            *theResult = 0;
+        }
+
+        return S_OK;
+
+    case CDDS_ITEMPOSTPAINT:
+
+        // Fetch the button style
+        btni.cbSize = sizeof(btni);
+        btni.dwMask = TBIF_STYLE;
+        SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, cdraw->nmcd.dwItemSpec, reinterpret_cast<LPARAM>(&btni));
+
+        // Check if we need to draw a submenu arrow
+        if (btni.fsStyle & BTNS_DROPDOWN)
+        {
+            // TODO: Support RTL text modes by drawing a leftwards arrow aligned to the left of the control
+
+            // "8" is the rightwards dropdown arrow in the Marlett font
+            WCHAR text [] = L"8";
+
+            // Configure the font to draw with Marlett, keeping the current background color as-is
+            SelectObject(cdraw->nmcd.hdc, m_marlett);
+            SetBkMode(cdraw->nmcd.hdc, TRANSPARENT);
+
+            // Tweak the alignment by 1 pixel so the menu draws like the Windows start menu.
+            RECT rc = cdraw->nmcd.rc;
+            rc.right += 1;
+
+            // The arrow is drawn at the right of the item's rect, aligned vertically.
+            DrawTextEx(cdraw->nmcd.hdc, text, 1, &rc, DT_NOCLIP | DT_VCENTER | DT_RIGHT | DT_SINGLELINE, NULL);
+        }
+        *theResult = TRUE;
+        return S_OK;
+    }
+    return S_OK;
+}
 
 CMenuToolbarBase::CMenuToolbarBase(CMenuBand *menuBand, BOOL usePager) :
     m_hwnd(NULL),
-    m_menuBand(menuBand),
     m_hwndToolbar(NULL),
+    m_useFlatMenus(FALSE),
+    m_SubclassOld(NULL),
+    m_disableMouseTrack(FALSE),
+    m_timerEnabled(FALSE),
+    m_menuBand(menuBand),
     m_dwMenuFlags(0),
-    m_hasIdealSize(FALSE)
+    m_hasSizes(FALSE),
+    m_usePager(usePager),
+    m_hotItem(-1),
+    m_popupItem(-1),
+    m_isTrackingPopup(FALSE)
 {
+    m_idealSize.cx = 0;
+    m_idealSize.cy = 0;
+    m_itemSize.cx = 0;
+    m_itemSize.cy = 0;
+    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");
 }
 
-HRESULT CMenuToolbarBase::IsWindowOwner(HWND hwnd)
+CMenuToolbarBase::~CMenuToolbarBase()
 {
-    return (m_hwnd && m_hwnd == hwnd) ||
-           (m_hwndToolbar && m_hwndToolbar == hwnd) ? S_OK : S_FALSE;
+    if (m_hwndToolbar && m_hwndToolbar != m_hwnd)
+        DestroyWindow(m_hwndToolbar);
+
+    if (m_hwnd)
+        DestroyWindow(m_hwnd);
+
+    DeleteObject(m_marlett);
 }
 
 void CMenuToolbarBase::InvalidateDraw()
@@ -64,38 +322,61 @@ HRESULT CMenuToolbarBase::ShowWindow(BOOL fShow)
 {
     ::ShowWindow(m_hwnd, fShow ? SW_SHOW : SW_HIDE);
 
+    // Ensure that the right image list is assigned to the toolbar
+    UpdateImageLists();
+
+    // For custom-drawing
+    SystemParametersInfo(SPI_GETFLATMENU, 0, &m_useFlatMenus, 0);
+
+    return S_OK;
+}
+
+HRESULT CMenuToolbarBase::UpdateImageLists()
+{
+    if ((m_initFlags & (SMINIT_TOPLEVEL | SMINIT_VERTICAL)) == SMINIT_TOPLEVEL) // not vertical.
+    {
+        // No image list, prevents the buttons from having a margin at the left side
+        SendMessageW(m_hwnd, TB_SETIMAGELIST, 0, 0);
+        return S_OK;
+    }
+
+    // Assign the correct imagelist and padding based on the current icon size
+
     int shiml;
     if (m_menuBand->UseBigIcons())
     {
         shiml = SHIL_LARGE;
-        SendMessageW(m_hwndToolbar, TB_SETPADDING, 0, MAKELPARAM(0, 0));
+        SendMessageW(m_hwndToolbar, TB_SETPADDING, 0, MAKELPARAM(4, 0));
     }
     else
     {
         shiml = SHIL_SMALL;
+        SendMessageW(m_hwndToolbar, TB_SETPADDING, 0, MAKELPARAM(4, 4));
     }
 
     IImageList * piml;
     HRESULT hr = SHGetImageList(shiml, IID_PPV_ARG(IImageList, &piml));
-    if (SUCCEEDED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
     {
-        SendMessageW(m_hwndToolbar, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(piml));
+        SendMessageW(m_hwndToolbar, TB_SETIMAGELIST, 0, 0);
     }
     else
     {
-        SendMessageW(m_hwndToolbar, TB_SETIMAGELIST, 0, 0);
+        SendMessageW(m_hwndToolbar, TB_SETIMAGELIST, 0, reinterpret_cast<LPARAM>(piml));
     }
-
     return S_OK;
 }
 
 HRESULT CMenuToolbarBase::Close()
 {
-    DestroyWindow(m_hwndToolbar);
     if (m_hwndToolbar != m_hwnd)
-        DestroyWindow(m_hwnd);
+        DestroyWindow(m_hwndToolbar);
+
+    DestroyWindow(m_hwnd);
+
     m_hwndToolbar = NULL;
     m_hwnd = NULL;
+
     return S_OK;
 }
 
@@ -104,17 +385,26 @@ 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 | TBSTYLE_CUSTOMERASE |
         CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NORESIZE | CCS_TOP;
-    LONG tbExStyles = TBSTYLE_EX_DOUBLEBUFFER;
+    LONG tbExStyles = TBSTYLE_EX_DOUBLEBUFFER | WS_EX_TOOLWINDOW;
 
     if (dwFlags & SMINIT_VERTICAL)
     {
+        // Activate vertical semantics
         tbStyles |= CCS_VERT;
-        tbExStyles |= TBSTYLE_EX_VERTICAL | WS_EX_TOOLWINDOW;
+
+#if USE_TBSTYLE_EX_VERTICAL
+        tbExStyles |= TBSTYLE_EX_VERTICAL;
+#endif
     }
 
-    RECT rc;
+    m_initFlags = dwFlags;
 
-    if (!::GetClientRect(hwndParent, &rc) || (rc.left == rc.right) || (rc.top == rc.bottom))
+    // Get a temporary rect to use while creating the toolbar window.
+    // Ensure that it is not a null rect.
+    RECT rc;
+    if (!::GetClientRect(hwndParent, &rc) ||
+        (rc.left == rc.right) ||
+        (rc.top == rc.bottom))
     {
         rc.left = 0;
         rc.top = 0;
@@ -130,6 +420,7 @@ HRESULT CMenuToolbarBase::CreateToolbar(HWND hwndParent, DWORD dwFlags)
     if (hwndToolbar == NULL)
         return E_FAIL;
 
+    // If needed, create the pager.
     if (m_usePager)
     {
         LONG pgStyles = PGS_VERT | WS_CHILD | WS_VISIBLE;
@@ -153,72 +444,84 @@ HRESULT CMenuToolbarBase::CreateToolbar(HWND hwndParent, DWORD dwFlags)
         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);
-
-    //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;
-    }
 
-    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);
-    }
+    // Identify the version of the used Common Controls DLL by sending the size of the TBBUTTON structure.
+    SendMessageW(hwndToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
 
+    // Apply subclassing
     SetWindowLongPtr(hwndToolbar, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
     m_SubclassOld = (WNDPROC) SetWindowLongPtr(hwndToolbar, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(CMenuToolbarBase::s_SubclassProc));
 
+    // Configure the image lists
+    UpdateImageLists();
+
     return S_OK;
 }
 
-HRESULT CMenuToolbarBase::GetIdealSize(SIZE& size)
+HRESULT CMenuToolbarBase::GetSizes(SIZE* pMinSize, SIZE* pMaxSize, SIZE* pIntegralSize)
 {
-    size.cx = size.cy = 0;
+    if (pMinSize)
+        *pMinSize = m_idealSize;
+    if (pMaxSize)
+        *pMaxSize = m_idealSize;
+    if (pIntegralSize)
+        *pIntegralSize = m_itemSize;
+
+    if (m_hasSizes)
+        return S_OK;
 
-    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;
-    }
+    if (!m_hwndToolbar)
+        return S_OK;
+
+    // Obtain the ideal size, to be used as min and max
+    SendMessageW(m_hwndToolbar, TB_AUTOSIZE, 0, 0);
+    SendMessageW(m_hwndToolbar, TB_GETMAXSIZE, 0, reinterpret_cast<LPARAM>(&m_idealSize));
+    SendMessageW(m_hwndToolbar, TB_GETIDEALSIZE, (m_initFlags & SMINIT_VERTICAL) != 0, reinterpret_cast<LPARAM>(&m_idealSize));
 
-    size = m_idealSize;
+    // Obtain the button size, to be used as the integral size
+    DWORD size = SendMessageW(m_hwndToolbar, TB_GETBUTTONSIZE, 0, 0);
+    m_itemSize.cx = GET_X_LPARAM(size);
+    m_itemSize.cy = GET_Y_LPARAM(size);
+    m_hasSizes = TRUE;
+
+    if (pMinSize)
+        *pMinSize = m_idealSize;
+    if (pMaxSize)
+        *pMaxSize = m_idealSize;
+    if (pIntegralSize)
+        *pIntegralSize = m_itemSize;
 
     return S_OK;
 }
 
 HRESULT CMenuToolbarBase::SetPosSize(int x, int y, int cx, int cy)
 {
+    // If we have a pager, set the toolbar height to the ideal height of the toolbar
     if (m_hwnd != m_hwndToolbar)
     {
         SetWindowPos(m_hwndToolbar, NULL, x, y, cx, m_idealSize.cy, 0);
     }
+
+    // Update the toolbar or pager to fit the requested rect
     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)));
+
+    // In a vertical menu, resize the buttons to fit the width
+    if (m_initFlags & SMINIT_VERTICAL)
+    {
+        DWORD btnSize = SendMessage(m_hwndToolbar, TB_GETBUTTONSIZE, 0, 0);
+        SendMessage(m_hwndToolbar, TB_SETBUTTONSIZE, 0, MAKELPARAM(cx, HIWORD(btnSize)));
+    }
+
     return S_OK;
 }
 
+HRESULT CMenuToolbarBase::IsWindowOwner(HWND hwnd)
+{
+    if (m_hwnd && m_hwnd == hwnd) return S_OK;
+    if (m_hwndToolbar && m_hwndToolbar == hwnd) return S_OK;
+    return S_FALSE;
+}
+
 HRESULT CMenuToolbarBase::GetWindow(HWND *phwnd)
 {
     if (!phwnd)
@@ -229,187 +532,345 @@ HRESULT CMenuToolbarBase::GetWindow(HWND *phwnd)
     return S_OK;
 }
 
-LRESULT CALLBACK CMenuToolbarBase::s_SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+HRESULT CMenuToolbarBase::OnGetInfoTip(NMTBGETINFOTIP * tip)
 {
-    CMenuToolbarBase * pthis = reinterpret_cast<CMenuToolbarBase *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
-    return pthis->SubclassProc(hWnd, uMsg, wParam, lParam);
+    INT index;
+    DWORD_PTR dwData;
+
+    INT iItem = tip->iItem;
+
+    GetDataFromId(iItem, &index, &dwData);
+
+    return InternalGetTooltip(iItem, index, dwData, tip->pszText, tip->cchTextMax);
 }
 
-LRESULT CMenuToolbarBase::SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+HRESULT CMenuToolbarBase::OnPopupTimer(DWORD timerId)
 {
-    switch (uMsg)
+    if (timerId != TIMERID_HOTTRACK)
+        return S_FALSE;
+
+    KillTimer(m_hwndToolbar, TIMERID_HOTTRACK);
+
+    if (!m_timerEnabled)
+        return S_FALSE;
+
+    m_timerEnabled = FALSE;
+
+    if (m_hotItem < 0)
+        return S_FALSE;
+
+    // Returns S_FALSE if the current item did not show a submenu
+    HRESULT hr = PopupItem(m_hotItem, FALSE);
+    if (hr != S_FALSE)
+        return hr;
+
+    // If we didn't switch submenus, cancel the current popup regardless
+    if (m_popupBar)
     {
-    case WM_TIMER:
-        if (wParam == TIMERID_HOTTRACK)
-        {
-            KillTimer(hWnd, TIMERID_HOTTRACK);
+        HRESULT hr = CancelCurrentPopup();
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+    }
 
-            m_menuBand->_OnPopupSubMenu(-1, NULL, NULL, NULL);
+    return S_OK;
+}
+
+HRESULT CMenuToolbarBase::KillPopupTimer()
+{
+    if (m_timerEnabled)
+    {
+        m_timerEnabled = FALSE;
+        KillTimer(m_hwndToolbar, TIMERID_HOTTRACK);
+        return S_OK;
+    }
+    return S_FALSE;
+}
+
+HRESULT CMenuToolbarBase::ChangeHotItem(CMenuToolbarBase * toolbar, INT item, DWORD dwFlags)
+{
+    // Ignore the change if it already matches the stored info
+    if (m_hotBar == toolbar && m_hotItem == item)
+        return S_FALSE;
+
+    // Prevent a change of hot item if the change was triggered by the mouse,
+    // and mouse tracking is disabled.
+    if (m_disableMouseTrack && dwFlags & HICF_MOUSE)
+    {
+        TRACE("Hot item change prevented by DisableMouseTrack\n");
+        return S_OK;
+    }
 
-            if (HasSubMenu(m_hotItem) == S_OK)
+    // Notify the toolbar if the hot-tracking left this toolbar
+    if (m_hotBar == this && toolbar != this)
+    {
+        SendMessage(m_hwndToolbar, TB_SETHOTITEM, (WPARAM) -1, 0);
+    }
+
+    TRACE("Hot item changed from %p %p, to %p %p\n", m_hotBar, m_hotItem, toolbar, item);
+    m_hotBar = toolbar;
+    m_hotItem = item;
+
+    if (m_hotBar == this)
+    {
+        if (m_isTrackingPopup && !(m_initFlags & SMINIT_VERTICAL))
+        {
+            // If the menubar has an open submenu, switch to the new item's submenu immediately
+            PopupItem(m_hotItem, FALSE);
+        }
+        else if (dwFlags & HICF_MOUSE)
+        {
+            // Vertical menus show/hide the submenu after a delay,
+            // but only with the mouse.
+            if (m_initFlags & SMINIT_VERTICAL)
             {
-                PopupItem(m_hotItem);
+                DWORD elapsed = 0;
+                SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &elapsed, 0);
+                SetTimer(m_hwndToolbar, TIMERID_HOTTRACK, elapsed, NULL);
+                m_timerEnabled = TRUE;
+                TRACE("SetTimer called with m_hotItem=%d\n", m_hotItem);
             }
         }
+        else
+        {
+            TBBUTTONINFO info;
+            info.cbSize = sizeof(info);
+            info.dwMask = 0;
+
+            int index = SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, item, reinterpret_cast<LPARAM>(&info));
+
+            SendMessage(m_hwndToolbar, TB_SETHOTITEM, index, 0);
+        }
     }
 
-    return m_SubclassOld(hWnd, uMsg, wParam, lParam);
+    InvalidateDraw();
+    return S_OK;
 }
 
-HRESULT CMenuToolbarBase::OnHotItemChange(const NMTBHOTITEM * hot)
+HRESULT CMenuToolbarBase::ChangePopupItem(CMenuToolbarBase * toolbar, INT item)
 {
-    if (hot->dwFlags & HICF_LEAVING)
+    // Ignore the change if it already matches the stored info
+    if (m_popupBar == toolbar && m_popupItem == item)
+        return S_FALSE;
+
+    // Notify the toolbar if the popup-tracking this toolbar
+    if (m_popupBar == this && toolbar != this)
     {
-        KillTimer(m_hwndToolbar, TIMERID_HOTTRACK);
-        m_hotItem = -1;
-        m_menuBand->_OnHotItemChanged(NULL, -1);
-        m_menuBand->_MenuItemHotTrack(MPOS_CHILDTRACKING);
+        SendMessage(m_hwndToolbar, TB_CHECKBUTTON, m_popupItem, FALSE);
+        m_isTrackingPopup = FALSE;
     }
-    else if (m_hotItem != hot->idNew)
-    {
-        DWORD elapsed = 0;
-        SystemParametersInfo(SPI_GETMENUSHOWDELAY, 0, &elapsed, 0);
-        SetTimer(m_hwndToolbar, TIMERID_HOTTRACK, elapsed, NULL);
 
-        m_hotItem = hot->idNew;
-        m_menuBand->_OnHotItemChanged(this, m_hotItem);
-        m_menuBand->_MenuItemHotTrack(MPOS_CHILDTRACKING);
+    m_popupBar = toolbar;
+    m_popupItem = item;
+
+    if (m_popupBar == this)
+    {
+        SendMessage(m_hwndToolbar, TB_CHECKBUTTON, m_popupItem, TRUE);
     }
+
+    InvalidateDraw();
     return S_OK;
 }
 
-HRESULT CMenuToolbarBase::PopupSubMenu(UINT itemId, UINT index, IShellMenu* childShellMenu)
+HRESULT CMenuToolbarBase::IsTrackedItem(INT index)
 {
-    IBandSite* pBandSite;
-    IDeskBar* pDeskBar;
+    TBBUTTON btn;
+
+    if (m_hotBar != this)
+        return S_FALSE;
+
+    if (index < 0)
+        return S_FALSE;
+
+    if (!SendMessage(m_hwndToolbar, TB_GETBUTTON, index, reinterpret_cast<LPARAM>(&btn)))
+        return E_FAIL;
+
+    if (m_hotItem == btn.idCommand)
+        return S_OK;
+
+    if (m_popupItem == btn.idCommand)
+        return S_OK;
+
+    return S_FALSE;
+}
+
+HRESULT CMenuToolbarBase::ChangeTrackedItem(INT index, BOOL wasTracking, BOOL mouse)
+{
+    TBBUTTON btn;
+
+    if (index < 0)
+    {
+        m_isTrackingPopup = FALSE;
+        return m_menuBand->_ChangeHotItem(NULL, -1, HICF_MOUSE);
+    }
+
+    if (!SendMessage(m_hwndToolbar, TB_GETBUTTON, index, reinterpret_cast<LPARAM>(&btn)))
+        return E_FAIL;
 
-    HRESULT hr = 0;
+    TRACE("ChangeTrackedItem %d, %d\n", index, wasTracking);
+    m_isTrackingPopup = wasTracking;
+    return m_menuBand->_ChangeHotItem(this, btn.idCommand, mouse ? HICF_MOUSE : 0);
+}
+
+HRESULT CMenuToolbarBase::PopupSubMenu(UINT iItem, UINT index, IShellMenu* childShellMenu, BOOL keyInitiated)
+{
+    // Calculate the submenu position and exclude area
     RECT rc = { 0 };
+    RECT rcx = { 0 };
 
     if (!SendMessage(m_hwndToolbar, TB_GETITEMRECT, index, reinterpret_cast<LPARAM>(&rc)))
         return E_FAIL;
 
+    GetWindowRect(m_hwnd, &rcx);
+
     POINT a = { rc.left, rc.top };
     POINT b = { rc.right, rc.bottom };
+    POINT c = { rcx.left, rcx.top };
+    POINT d = { rcx.right, rcx.bottom };
 
     ClientToScreen(m_hwndToolbar, &a);
     ClientToScreen(m_hwndToolbar, &b);
+    ClientToScreen(m_hwnd, &c);
+    ClientToScreen(m_hwnd, &d);
 
-    POINTL pt = { b.x - 4, a.y };
-    RECTL rcl = { a.x, a.y, b.x, b.y }; // maybe-TODO: fetch client area of deskbar?
-
-
-#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
-
-#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
-
-    hr = pDeskBar->SetClient(pBandSite);
-    if (FAILED(hr))
-        return hr;
+    POINTL pt = { a.x, b.y };
+    RECTL rcl = { c.x, c.y, d.x, d.y };
 
-    hr = pBandSite->AddBand(childShellMenu);
-    if (FAILED(hr))
-        return hr;
+    if (m_initFlags & SMINIT_VERTICAL)
+    {
+        pt.x = b.x - 3;
+        pt.y = a.y - 3;
+    }
 
-    CComPtr<IMenuPopup> popup;
-    hr = pDeskBar->QueryInterface(IID_PPV_ARG(IMenuPopup, &popup));
-    if (FAILED(hr))
-        return hr;
+    // Display the submenu
+    m_isTrackingPopup = TRUE;
 
-    m_menuBand->_OnPopupSubMenu(itemId, popup, &pt, &rcl);
+    m_menuBand->_ChangePopupItem(this, iItem);
+    m_menuBand->_OnPopupSubMenu(childShellMenu, &pt, &rcl, keyInitiated);
 
     return S_OK;
 }
 
-HRESULT CMenuToolbarBase::PopupSubMenu(UINT index, HMENU menu)
+HRESULT CMenuToolbarBase::PopupSubMenu(UINT iItem, UINT index, HMENU menu)
 {
+    // Calculate the submenu position and exclude area
     RECT rc = { 0 };
 
     if (!SendMessage(m_hwndToolbar, TB_GETITEMRECT, index, reinterpret_cast<LPARAM>(&rc)))
         return E_FAIL;
 
+    POINT a = { rc.left, rc.top };
     POINT b = { rc.right, rc.bottom };
 
+    ClientToScreen(m_hwndToolbar, &a);
     ClientToScreen(m_hwndToolbar, &b);
 
+    POINT pt = { a.x, b.y };
+    RECT rcl = { a.x, a.y, b.x, b.y };
+
+    if (m_initFlags & SMINIT_VERTICAL)
+    {
+        pt.x = b.x;
+        pt.y = a.y;
+    }
+
     HMENU popup = GetSubMenu(menu, index);
 
-    m_menuBand->_TrackSubMenuUsingTrackPopupMenu(popup, b.x, b.y);
+    // Display the submenu
+    m_isTrackingPopup = TRUE;
+    m_menuBand->_ChangePopupItem(this, iItem);
+    m_menuBand->_TrackSubMenu(popup, pt.x, pt.y, rcl);
+    m_menuBand->_ChangePopupItem(NULL, -1);
+    m_isTrackingPopup = FALSE;
+
+    m_menuBand->_ChangeHotItem(NULL, -1, 0);
 
     return S_OK;
 }
 
-HRESULT CMenuToolbarBase::DoContextMenu(IContextMenu* contextMenu)
+HRESULT CMenuToolbarBase::TrackContextMenu(IContextMenu* contextMenu, POINT pt)
 {
-    HRESULT hr;
-    HMENU hPopup = CreatePopupMenu();
+    // Cancel submenus
+    m_menuBand->_KillPopupTimers();
+    if (m_popupBar)
+        m_menuBand->_CancelCurrentPopup();
 
-    if (hPopup == NULL)
-        return E_FAIL;
+    // Display the context menu
+    return m_menuBand->_TrackContextMenu(contextMenu, pt.x, pt.y);
+}
+
+HRESULT CMenuToolbarBase::ProcessClick(INT iItem)
+{
+    if (m_disableMouseTrack)
+    {
+        TRACE("Item click prevented by DisableMouseTrack\n");
+        return S_OK;
+    }
 
-    hr = contextMenu->QueryContextMenu(hPopup, 0, 0, UINT_MAX, CMF_NORMAL);
-    if (FAILED(hr))
+    // If a button is clicked while a submenu was open, cancel the submenu.
+    if (!(m_initFlags & SMINIT_VERTICAL) && m_isTrackingPopup)
     {
-        DestroyMenu(hPopup);
-        return hr;
+        TRACE("OnCommand cancelled because it was tracking submenu.\n");
+        return S_FALSE;
     }
 
-    DWORD dwPos = GetMessagePos();
-    UINT uCommand = ::TrackPopupMenu(hPopup, TPM_RETURNCMD, GET_X_LPARAM(dwPos), GET_Y_LPARAM(dwPos), 0, m_hwnd, NULL);
-    if (uCommand == 0)
+    if (PopupItem(iItem, FALSE) == S_OK)
+    {
+        TRACE("PopupItem returned S_OK\n");
         return S_FALSE;
+    }
 
-    CMINVOKECOMMANDINFO cmi = { 0 };
-    cmi.cbSize = sizeof(cmi);
-    cmi.lpVerb = MAKEINTRESOURCEA(uCommand);
-    cmi.hwnd = m_hwnd;
-    hr = contextMenu->InvokeCommand(&cmi);
+    TRACE("Executing...\n");
 
-    DestroyMenu(hPopup);
-    return hr;
+    return m_menuBand->_MenuItemHotTrack(MPOS_EXECUTE);
 }
 
-HRESULT CMenuToolbarBase::OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult)
+HRESULT CMenuToolbarBase::MenuBarMouseDown(INT iIndex)
 {
-    theResult = 0;
-    if (HasSubMenu(wParam) == S_OK)
-    {
-        KillTimer(m_hwndToolbar, TIMERID_HOTTRACK);
-        PopupItem(wParam);
-        return S_FALSE;
-    }
-    return m_menuBand->_MenuItemHotTrack(MPOS_EXECUTE);
+    TBBUTTON btn;
+
+    if (m_initFlags & SMINIT_VERTICAL)
+        return S_OK;
+
+    ::SendMessageW(m_hwndToolbar, TB_GETBUTTON, iIndex, reinterpret_cast<LPARAM>(&btn));
+    return ProcessClick(btn.idCommand);
+}
+
+HRESULT CMenuToolbarBase::MenuBarMouseUp(INT iIndex)
+{
+    TBBUTTON btn;
+
+    if (!(m_initFlags & SMINIT_VERTICAL))
+        return S_OK;
+
+    ::SendMessageW(m_hwndToolbar, TB_GETBUTTON, iIndex, reinterpret_cast<LPARAM>(&btn));
+    return ProcessClick(btn.idCommand);
+}
+
+HRESULT CMenuToolbarBase::PrepareExecuteItem(INT iItem)
+{
+    this->m_menuBand->_KillPopupTimers();
+
+    m_executeItem = iItem;
+    return GetDataFromId(iItem, &m_executeIndex, &m_executeData);
+}
+
+HRESULT CMenuToolbarBase::ExecuteItem()
+{
+    return InternalExecuteItem(m_executeItem, m_executeItem, m_executeData);
+}
+
+HRESULT CMenuToolbarBase::OnContextMenu(NMMOUSE * rclick)
+{
+    INT iItem = rclick->dwItemSpec;
+    INT index = rclick->dwHitInfo;
+    DWORD_PTR data = rclick->dwItemData;
+
+    GetDataFromId(iItem, &index, &data);
+
+    return InternalContextMenu(iItem, index, data, rclick->pt);
 }
 
-HRESULT CMenuToolbarBase::ChangeHotItem(DWORD dwSelectType)
+HRESULT CMenuToolbarBase::KeyboardItemChange(DWORD dwSelectType)
 {
     int prev = m_hotItem;
     int index = -1;
@@ -418,14 +879,6 @@ HRESULT CMenuToolbarBase::ChangeHotItem(DWORD dwSelectType)
     {
         int count = SendMessage(m_hwndToolbar, TB_BUTTONCOUNT, 0, 0);
 
-        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));
-        }
-
         if (dwSelectType == VK_HOME)
         {
             index = 0;
@@ -436,26 +889,37 @@ HRESULT CMenuToolbarBase::ChangeHotItem(DWORD dwSelectType)
             index = count - 1;
             dwSelectType = VK_UP;
         }
-        else if (index < 0)
+        else
         {
-            if (dwSelectType == VK_UP)
-            {
-                index = count - 1;
-            }
-            else if (dwSelectType == VK_DOWN)
+            if (m_hotItem >= 0)
             {
-                index = 0;
+                TBBUTTONINFO info = { 0 };
+                info.cbSize = sizeof(TBBUTTONINFO);
+                info.dwMask = 0;
+                index = SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, m_hotItem, reinterpret_cast<LPARAM>(&info));
             }
-        }
-        else
-        {
-            if (dwSelectType == VK_UP)
+
+            if (index < 0)
             {
-                index--;
+                if (dwSelectType == VK_UP)
+                {
+                    index = count - 1;
+                }
+                else if (dwSelectType == VK_DOWN)
+                {
+                    index = 0;
+                }
             }
-            else if (dwSelectType == VK_DOWN)
+            else
             {
-                index++;
+                if (dwSelectType == VK_UP)
+                {
+                    index--;
+                }
+                else if (dwSelectType == VK_DOWN)
+                {
+                    index++;
+                }
             }
         }
 
@@ -468,11 +932,18 @@ HRESULT CMenuToolbarBase::ChangeHotItem(DWORD dwSelectType)
 
             if (btn.dwData)
             {
-                m_hotItem = btn.idCommand;
-                if (prev != m_hotItem)
+                if (prev != btn.idCommand)
                 {
-                    SendMessage(m_hwndToolbar, TB_SETHOTITEM, index, 0);
-                    return m_menuBand->_OnHotItemChanged(this, m_hotItem);
+                    TRACE("Setting Hot item to %d\n", index);
+                    if (!(m_initFlags & SMINIT_VERTICAL) && m_isTrackingPopup)
+                    {
+                        HWND tlw;
+                        m_menuBand->_GetTopLevelWindow(&tlw);
+                        SendMessage(tlw, WM_CANCELMODE, 0, 0);
+                        PostMessage(m_hwndToolbar, WM_USER_CHANGETRACKEDITEM, index, MAKELPARAM(m_isTrackingPopup, FALSE));
+                    }
+                    else
+                        m_menuBand->_ChangeHotItem(this, btn.idCommand, 0);
                 }
                 return S_OK;
             }
@@ -486,34 +957,156 @@ HRESULT CMenuToolbarBase::ChangeHotItem(DWORD dwSelectType)
                 index++;
             }
         }
+
+        return S_FALSE;
     }
 
-    m_hotItem = -1;
-    if (prev != m_hotItem)
+    if (prev != -1)
     {
-        SendMessage(m_hwndToolbar, TB_SETHOTITEM, -1, 0);
-        m_menuBand->_OnHotItemChanged(NULL, -1);
+        TRACE("Setting Hot item to null\n");
+        m_menuBand->_ChangeHotItem(NULL, -1, 0);
     }
+
     return S_FALSE;
 }
 
-BOOL
-AllocAndGetMenuString(HMENU hMenu, UINT ItemIDByPosition, WCHAR** String)
+HRESULT CMenuToolbarBase::AddButton(DWORD commandId, LPCWSTR caption, BOOL hasSubMenu, INT iconId, DWORD_PTR buttonData, BOOL last)
 {
-    int Length;
+    TBBUTTON tbb = { 0 };
 
-    Length = GetMenuStringW(hMenu, ItemIDByPosition, NULL, 0, MF_BYPOSITION);
+    tbb.fsState = TBSTATE_ENABLED;
+#if !USE_TBSTYLE_EX_VERTICAL
+    if (!last && (m_initFlags & SMINIT_VERTICAL))
+        tbb.fsState |= TBSTATE_WRAP;
+#endif
+    tbb.fsStyle = BTNS_CHECKGROUP;
+
+    if (hasSubMenu && (m_initFlags & SMINIT_VERTICAL))
+        tbb.fsStyle |= BTNS_DROPDOWN;
+
+    if (!(m_initFlags & SMINIT_VERTICAL))
+        tbb.fsStyle |= BTNS_AUTOSIZE;
+
+    tbb.iString = (INT_PTR) caption;
+    tbb.idCommand = commandId;
+
+    tbb.iBitmap = iconId;
+    tbb.dwData = buttonData;
+
+    if (!SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb)))
+        return HRESULT_FROM_WIN32(GetLastError());
+
+    return S_OK;
+}
+
+HRESULT CMenuToolbarBase::AddSeparator(BOOL last)
+{
+    TBBUTTON tbb = { 0 };
+
+    tbb.fsState = TBSTATE_ENABLED;
+#if !USE_TBSTYLE_EX_VERTICAL
+    if (!last && (m_initFlags & SMINIT_VERTICAL))
+        tbb.fsState |= TBSTATE_WRAP;
+#endif
+    tbb.fsStyle = BTNS_SEP;
+    tbb.iBitmap = 0;
+
+    if (!SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb)))
+        return HRESULT_FROM_WIN32(GetLastError());
+
+    return S_OK;
+}
+
+HRESULT CMenuToolbarBase::AddPlaceholder()
+{
+    TBBUTTON tbb = { 0 };
+    PCWSTR MenuString = L"(Empty)";
 
-    if (!Length)
-        return FALSE;
+    tbb.fsState = 0;
+    tbb.fsStyle = 0;
+    tbb.iString = (INT_PTR) MenuString;
+    tbb.iBitmap = -1;
 
-    /* Also allocate space for the terminating NULL character */
-    ++Length;
-    *String = (PWSTR) HeapAlloc(GetProcessHeap(), 0, Length * sizeof(WCHAR));
+    if (!SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb)))
+        return HRESULT_FROM_WIN32(GetLastError());
 
-    GetMenuStringW(hMenu, ItemIDByPosition, *String, Length, MF_BYPOSITION);
+    return S_OK;
+}
 
-    return TRUE;
+HRESULT CMenuToolbarBase::ClearToolbar()
+{
+    while (SendMessage(m_hwndToolbar, TB_DELETEBUTTON, 0, 0))
+    {
+        // empty;
+    }
+    return S_OK;
+}
+
+HRESULT CMenuToolbarBase::GetDataFromId(INT iItem, INT* pIndex, DWORD_PTR* pData)
+{
+    if (pData)
+        *pData = NULL;
+
+    if (pIndex)
+        *pIndex = -1;
+
+    if (iItem < 0)
+        return S_OK;
+
+    TBBUTTONINFO info = { 0 };
+
+    info.cbSize = sizeof(TBBUTTONINFO);
+    info.dwMask = TBIF_COMMAND | TBIF_LPARAM;
+
+    int index = SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, iItem, reinterpret_cast<LPARAM>(&info));
+    if (index < 0)
+        return E_FAIL;
+
+    if (pIndex)
+        *pIndex = index;
+
+    if (pData)
+        *pData = info.lParam;
+
+    return S_OK;
+}
+
+HRESULT CMenuToolbarBase::CancelCurrentPopup()
+{
+    return m_menuBand->_CancelCurrentPopup();
+}
+
+HRESULT CMenuToolbarBase::PopupItem(INT iItem, BOOL keyInitiated)
+{
+    INT index;
+    DWORD_PTR dwData;
+
+    if (iItem < 0)
+        return S_OK;
+
+    if (m_popupBar == this && m_popupItem == iItem)
+        return S_OK;
+
+    GetDataFromId(iItem, &index, &dwData);
+
+    HRESULT hr = InternalHasSubMenu(iItem, index, dwData);
+    if (hr != S_OK)
+        return hr;
+
+    if (m_popupBar)
+    {
+        HRESULT hr = CancelCurrentPopup();
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+    }
+
+    if (!(m_initFlags & SMINIT_VERTICAL))
+    {
+        TRACE("PopupItem non-vertical %d %d\n", index, iItem);
+        m_menuBand->_ChangeHotItem(this, iItem, 0);
+    }
+
+    return InternalPopupItem(iItem, index, dwData, keyInitiated);
 }
 
 CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand *menuBand) :
@@ -522,14 +1115,21 @@ CMenuStaticToolbar::CMenuStaticToolbar(CMenuBand *menuBand) :
 {
 }
 
+CMenuStaticToolbar::~CMenuStaticToolbar()
+{
+}
+
 HRESULT  CMenuStaticToolbar::GetMenu(
-    HMENU *phmenu,
-    HWND *phwnd,
-    DWORD *pdwFlags)
+    _Out_opt_ HMENU *phmenu,
+    _Out_opt_ HWND *phwnd,
+    _Out_opt_ DWORD *pdwFlags)
 {
-    *phmenu = m_hmenu;
-    *phwnd = NULL;
-    *pdwFlags = m_dwMenuFlags;
+    if (phmenu)
+        *phmenu = m_hmenu;
+    if (phwnd)
+        *phwnd = NULL;
+    if (pdwFlags)
+        *pdwFlags = m_dwMenuFlags;
 
     return S_OK;
 }
@@ -545,126 +1145,127 @@ HRESULT  CMenuStaticToolbar::SetMenu(
     return S_OK;
 }
 
-HRESULT CMenuStaticToolbar::FillToolbar()
+HRESULT CMenuStaticToolbar::FillToolbar(BOOL clearFirst)
 {
     int i;
     int ic = GetMenuItemCount(m_hmenu);
 
+    if (clearFirst)
+    {
+        ClearToolbar();
+    }
+
+    int count = 0;
     for (i = 0; i < ic; i++)
     {
-        MENUITEMINFOW info;
-        TBBUTTON tbb = { 0 };
-        PWSTR MenuString = NULL;
+        BOOL last = i + 1 == ic;
 
-        tbb.fsState = TBSTATE_ENABLED;
-        tbb.fsStyle = 0;
+        MENUITEMINFOW info;
 
         info.cbSize = sizeof(info);
-        info.fMask = MIIM_FTYPE | MIIM_ID;
+        info.dwTypeData = NULL;
+        info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_ID;
+
+        if (!GetMenuItemInfoW(m_hmenu, i, TRUE, &info))
+        {
+            TRACE("Error obtaining info for menu item at pos=%d\n", i);
+            continue;
+        }
 
-        GetMenuItemInfoW(m_hmenu, i, TRUE, &info);
+        count++;
 
-        if (info.fType == MFT_STRING)
+        if (info.fType & MFT_SEPARATOR)
         {
-            if (!AllocAndGetMenuString(m_hmenu, i, &MenuString))
-                return E_OUTOFMEMORY;
-            if (::GetSubMenu(m_hmenu, i) != NULL)
-                tbb.fsStyle |= BTNS_DROPDOWN;
-            tbb.iString = (INT_PTR) MenuString;
-            tbb.idCommand = info.wID;
+            AddSeparator(last);
+        }
+        else if (!(info.fType & MFT_BITMAP))
+        {
+            info.cch++;
+            info.dwTypeData = (PWSTR) HeapAlloc(GetProcessHeap(), 0, (info.cch + 1) * sizeof(WCHAR));
+
+            info.fMask = MIIM_STRING | MIIM_SUBMENU | MIIM_ID;
+            GetMenuItemInfoW(m_hmenu, i, TRUE, &info);
 
             SMINFO * sminfo = new SMINFO();
             sminfo->dwMask = SMIM_ICON | SMIM_FLAGS;
-            if (SUCCEEDED(m_menuBand->_CallCBWithItemId(info.wID, SMC_GETINFO, 0, reinterpret_cast<LPARAM>(sminfo))))
+            // FIXME: remove before deleting the toolbar or it will leak
+
+            HRESULT hr = m_menuBand->_CallCBWithItemId(info.wID, SMC_GETINFO, 0, reinterpret_cast<LPARAM>(sminfo));
+            if (FAILED_UNEXPECTEDLY(hr))
             {
-                tbb.iBitmap = sminfo->iIcon;
-                tbb.dwData = reinterpret_cast<DWORD_PTR>(sminfo);
-                // FIXME: remove before deleting the toolbar or it will leak
+                delete sminfo;
+                return hr;
             }
-        }
-        else
-        {
-            tbb.fsStyle |= BTNS_SEP;
-        }
 
-        SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb));
+            AddButton(info.wID, info.dwTypeData, info.hSubMenu != NULL, sminfo->iIcon, reinterpret_cast<DWORD_PTR>(sminfo), last);
 
-        if (MenuString)
-            HeapFree(GetProcessHeap(), 0, MenuString);
+            HeapFree(GetProcessHeap(), 0, info.dwTypeData);
+        }
     }
 
     return S_OK;
 }
 
-HRESULT CMenuStaticToolbar::OnContextMenu(NMMOUSE * rclick)
+HRESULT CMenuStaticToolbar::InternalGetTooltip(INT iItem, INT index, DWORD_PTR dwData, LPWSTR pszText, INT cchTextMax)
 {
-    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);
+    //SMINFO * info = reinterpret_cast<SMINFO*>(dwData);
+    UNIMPLEMENTED;
+    return E_NOTIMPL;
 }
 
-HRESULT CMenuStaticToolbar::OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult)
+HRESULT CMenuStaticToolbar::OnDeletingButton(const NMTOOLBAR * tb)
 {
-    HRESULT hr;
-    hr = CMenuToolbarBase::OnCommand(wParam, lParam, theResult);
-    if (FAILED(hr))
-        return hr;
+    delete reinterpret_cast<SMINFO*>(tb->tbButton.dwData);
+    return S_OK;
+}
 
-    // in case the clicked item has a submenu, we do not need to execute the item
-    if (hr == S_FALSE)
+HRESULT CMenuStaticToolbar::InternalContextMenu(INT iItem, INT index, DWORD_PTR dwData, POINT pt)
+{
+    CComPtr<IContextMenu> contextMenu;
+    HRESULT hr = m_menuBand->_CallCBWithItemId(iItem, SMC_GETOBJECT, 
+        reinterpret_cast<WPARAM>(&IID_IContextMenu), reinterpret_cast<LPARAM>(&contextMenu));
+    if (hr != S_OK)
         return hr;
 
-    return m_menuBand->_CallCBWithItemId(wParam, SMC_EXEC, 0, 0);
+    return TrackContextMenu(contextMenu, pt);
 }
 
-HRESULT CMenuStaticToolbar::PopupItem(UINT uItem)
+HRESULT CMenuStaticToolbar::InternalExecuteItem(INT iItem, INT index, DWORD_PTR data)
 {
-    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));
+    return m_menuBand->_CallCBWithItemId(iItem, SMC_EXEC, 0, 0);
+}
 
-    SMINFO * nfo = reinterpret_cast<SMINFO*>(btn.dwData);
+HRESULT CMenuStaticToolbar::InternalPopupItem(INT iItem, INT index, DWORD_PTR dwData, BOOL keyInitiated)
+{
+    SMINFO * nfo = reinterpret_cast<SMINFO*>(dwData);
     if (!nfo)
         return E_FAIL;
 
     if (nfo->dwFlags&SMIF_TRACKPOPUP)
     {
-        return PopupSubMenu(index, m_hmenu);
+        return PopupSubMenu(iItem, 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))
+        HRESULT hr = m_menuBand->_CallCBWithItemId(iItem, SMC_GETOBJECT, reinterpret_cast<WPARAM>(&IID_IShellMenu), reinterpret_cast<LPARAM>(&shellMenu));
+        if (FAILED_UNEXPECTEDLY(hr))
             return hr;
 
-        return PopupSubMenu(uItem, index, shellMenu);
+        return PopupSubMenu(iItem, index, shellMenu, keyInitiated);
     }
 }
 
-HRESULT CMenuStaticToolbar::HasSubMenu(UINT uItem)
+HRESULT CMenuStaticToolbar::InternalHasSubMenu(INT iItem, INT index, DWORD_PTR dwData)
 {
-    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, TRUE),
-    m_shellFolder(NULL)
+    m_shellFolder(NULL),
+    m_idList(NULL),
+    m_hKey(NULL)
 {
 }
 
@@ -672,80 +1273,82 @@ CMenuSFToolbar::~CMenuSFToolbar()
 {
 }
 
-HRESULT CMenuSFToolbar::FillToolbar()
+HRESULT CMenuSFToolbar::FillToolbar(BOOL clearFirst)
 {
     HRESULT hr;
     int i = 0;
     PWSTR MenuString;
 
     IEnumIDList * eidl;
-    m_shellFolder->EnumObjects(m_hwndToolbar, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &eidl);
+    m_shellFolder->EnumObjects(GetToolbar(), SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &eidl);
 
-    LPITEMIDLIST item = static_cast<LPITEMIDLIST>(CoTaskMemAlloc(sizeof(ITEMIDLIST)));
-    ULONG fetched;
-    while ((hr = eidl->Next(1, &item, &fetched)) == S_OK)
+    LPITEMIDLIST item = NULL;
+    hr = eidl->Next(1, &item, NULL);
+    while (hr == S_OK)
     {
         INT index = 0;
         INT indexOpen = 0;
 
-        TBBUTTON tbb = { 0 };
-        tbb.fsState = TBSTATE_ENABLED;
-        tbb.fsStyle = 0;
+        if (m_menuBand->_CallCBWithItemPidl(item, 0x10000000, 0, 0) == S_OK)
+        {
+            STRRET sr = { STRRET_CSTR, { 0 } };
+
+            hr = m_shellFolder->GetDisplayNameOf(item, SIGDN_NORMALDISPLAY, &sr);
+            if (FAILED_UNEXPECTEDLY(hr))
+                return hr;
 
-        CComPtr<IShellItem> psi;
-        hr = SHCreateShellItem(NULL, m_shellFolder, item, &psi);
-        if (FAILED(hr))
-            return hr;
+            StrRetToStr(&sr, NULL, &MenuString);
 
-        hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY, &MenuString);
-        if (FAILED(hr))
-            return hr;
+            index = SHMapPIDLToSystemImageListIndex(m_shellFolder, item, &indexOpen);
 
-        index = SHMapPIDLToSystemImageListIndex(m_shellFolder, item, &indexOpen);
+            LPCITEMIDLIST itemc = item;
 
-        SFGAOF attrs;
-        hr = psi->GetAttributes(SFGAO_FOLDER, &attrs);
+            SFGAOF attrs = SFGAO_FOLDER;
+            hr = m_shellFolder->GetAttributesOf(1, &itemc, &attrs);
 
-        if (attrs != 0)
-        {
-            tbb.fsStyle |= BTNS_DROPDOWN;
-        }
+            DWORD_PTR dwData = reinterpret_cast<DWORD_PTR>(ILClone(item));
 
-        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
+            // Fetch next item already, so we know if the current one is the last
+            hr = eidl->Next(1, &item, NULL);
 
-        SendMessageW(m_hwndToolbar, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&tbb));
-        HeapFree(GetProcessHeap(), 0, MenuString);
+            AddButton(++i, MenuString, attrs & SFGAO_FOLDER, index, dwData, hr != S_OK);
 
+            CoTaskMemFree(MenuString);
+        }
+        else
+        {
+            // Fetch next item here also
+            hr = eidl->Next(1, &item, NULL);
+        }
     }
-    CoTaskMemFree(item);
+    ILFree(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 AddPlaceholder();
     }
 
     return hr;
 }
 
+HRESULT CMenuSFToolbar::InternalGetTooltip(INT iItem, INT index, DWORD_PTR dwData, LPWSTR pszText, INT cchTextMax)
+{
+    //ITEMIDLIST * pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
+    UNIMPLEMENTED;
+    return E_NOTIMPL;
+}
+
+HRESULT CMenuSFToolbar::OnDeletingButton(const NMTOOLBAR * tb)
+{
+    ILFree(reinterpret_cast<LPITEMIDLIST>(tb->tbButton.dwData));
+    return S_OK;
+}
+
 HRESULT CMenuSFToolbar::SetShellFolder(IShellFolder *psf, LPCITEMIDLIST pidlFolder, HKEY hKey, DWORD dwFlags)
 {
     m_shellFolder = psf;
-    m_idList = pidlFolder;
+    m_idList = ILClone(pidlFolder);
     m_hKey = hKey;
     m_dwMenuFlags = dwFlags;
     return S_OK;
@@ -756,7 +1359,7 @@ HRESULT CMenuSFToolbar::GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REF
     HRESULT hr;
 
     hr = m_shellFolder->QueryInterface(riid, ppv);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     if (pdwFlags)
@@ -771,7 +1374,8 @@ HRESULT CMenuSFToolbar::GetShellFolder(DWORD *pdwFlags, LPITEMIDLIST *ppidl, REF
             pidl = ILClone(m_idList);
             if (!pidl)
             {
-                (*(IUnknown**) ppv)->Release();
+                ERR("ILClone failed!\n");
+                (*reinterpret_cast<IUnknown**>(ppv))->Release();
                 return E_FAIL;
             }
         }
@@ -782,114 +1386,74 @@ 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 CMenuSFToolbar::InternalContextMenu(INT iItem, INT index, DWORD_PTR dwData, POINT pt)
 {
     HRESULT hr;
-    CComPtr<IContextMenu> contextMenu;
-    LPCITEMIDLIST pidl = reinterpret_cast<LPCITEMIDLIST>(rclick->dwItemData);
+    CComPtr<IContextMenu> contextMenu = NULL;
+    LPCITEMIDLIST pidl = reinterpret_cast<LPCITEMIDLIST>(dwData);
 
-    hr = m_shellFolder->GetUIObjectOf(m_hwndToolbar, 1, &pidl, IID_IContextMenu, NULL, reinterpret_cast<VOID **>(&contextMenu));
-    if (hr != S_OK)
+    hr = m_shellFolder->GetUIObjectOf(GetToolbar(), 1, &pidl, IID_NULL_PPV_ARG(IContextMenu, &contextMenu));
+    if (FAILED_UNEXPECTEDLY(hr))
+    {
         return hr;
+    }
 
-    return DoContextMenu(contextMenu);
+    hr = TrackContextMenu(contextMenu, pt);
+
+    return hr;
 }
 
-HRESULT CMenuSFToolbar::OnCommand(WPARAM wParam, LPARAM lParam, LRESULT *theResult)
+HRESULT CMenuSFToolbar::InternalExecuteItem(INT iItem, INT index, DWORD_PTR data)
 {
-    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);
+    return m_menuBand->_CallCBWithItemPidl(reinterpret_cast<LPITEMIDLIST>(data), SMC_SFEXEC, 0, 0);
 }
 
-HRESULT CMenuSFToolbar::PopupItem(UINT uItem)
+HRESULT CMenuSFToolbar::InternalPopupItem(INT iItem, INT index, DWORD_PTR dwData, BOOL keyInitiated)
 {
     HRESULT hr;
     UINT uId;
     UINT uIdAncestor;
     DWORD flags;
-    int index;
     CComPtr<IShellMenuCallback> psmc;
     CComPtr<IShellMenu> shellMenu;
 
-    LPITEMIDLIST pidl = GetPidlFromId(uItem, &index);
+    LPITEMIDLIST pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
 
     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))
+    if (FAILED_UNEXPECTEDLY(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))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     CComPtr<IShellFolder> childFolder;
     hr = m_shellFolder->BindToObject(pidl, NULL, IID_PPV_ARG(IShellFolder, &childFolder));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     hr = shellMenu->SetShellFolder(childFolder, NULL, NULL, 0);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
-    return PopupSubMenu(uItem, index, shellMenu);
+    return PopupSubMenu(iItem, index, shellMenu, keyInitiated);
 }
 
-HRESULT CMenuSFToolbar::HasSubMenu(UINT uItem)
+HRESULT CMenuSFToolbar::InternalHasSubMenu(INT iItem, INT index, DWORD_PTR dwData)
 {
     HRESULT hr;
-    CComPtr<IShellItem> psi;
-    hr = SHCreateShellItem(NULL, m_shellFolder, GetPidlFromId(uItem), &psi);
-    if (FAILED(hr))
-        return S_FALSE;
+    LPCITEMIDLIST pidl = reinterpret_cast<LPITEMIDLIST>(dwData);
 
-    SFGAOF attrs;
-    hr = psi->GetAttributes(SFGAO_FOLDER, &attrs);
-    if (FAILED(hr))
+    SFGAOF attrs = SFGAO_FOLDER;
+    hr = m_shellFolder->GetAttributesOf(1, &pidl, &attrs);
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
-    return (attrs != 0) ? S_OK : S_FALSE;
+    return (attrs & SFGAO_FOLDER) ? S_OK : S_FALSE;
 }