BOOL CXTPRibbonControlTab::OnSetPopup(BOOL bPopup)
{
	m_bPopup = bPopup;
	if (bPopup)
	{
		ASSERT(GetSelectedItem());
		CXTPRibbonTab* pSelectedTab = (CXTPRibbonTab*)GetSelectedItem();
		if (!pSelectedTab || !GetRibbonBar()->IsRibbonMinimized())
		{
			m_bPopup = FALSE;
			return FALSE;
		}

		if (m_pCommandBar)
			m_pCommandBar->InternalRelease();

		CXTPCommandBars* pCommandBars = GetRibbonBar()->GetCommandBars();
		ASSERT(pCommandBars);

		m_pCommandBar = GetRibbonBar()->CreateTabPopupToolBar(pSelectedTab);
		m_pCommandBar->SetCommandBars(pCommandBars);

		m_pCommandBar->EnableCustomization(FALSE);
		m_pCommandBar->Popup(this, IsKeyboardSelected(GetSelected()));
	}
	else if (m_pCommandBar)
	{
		ASSERT(m_pCommandBar);
		m_pCommandBar->SetTrackingMode(FALSE);
	}
	RedrawParent();
	return TRUE;
}
BOOL CXTPRibbonGroupControlPopup::OnSetPopup(BOOL bPopup)
{
    m_bPopup = bPopup;
    if (bPopup)
    {
        CMDTARGET_RELEASE(m_pCommandBar);

        CXTPCommandBars* pCommandBars = m_pParent->GetCommandBars();
        ASSERT(pCommandBars);

        if (!m_pPopupGroup)
        {
            m_pCommandBar = new CXTPPopupToolBar();
            m_pCommandBar->SetCommandBars(pCommandBars);
            m_pCommandBar->Popup(this, IsKeyboardSelected(GetSelected()));
        }
        else
        {

            m_pCommandBar = new CXTPRibbonGroupPopupToolBar(m_pPopupGroup);
            m_pCommandBar->SetCommandBars(pCommandBars);

            m_pCommandBar->EnableCustomization(FALSE);
            m_pCommandBar->Popup(this, IsKeyboardSelected(GetSelected()));
        }
    }
    else
    {
        ASSERT(m_pCommandBar);
        if (m_pCommandBar) m_pCommandBar->SetTrackingMode(FALSE);

    }
    RedrawParent();
    return TRUE;
}
void CXTPControlScrollBar::OnScroll(UINT nSBCode, UINT nPos)
{
	int nCurPos = GetPos();

	switch (nSBCode)
	{
		case SB_TOP: nCurPos = m_nMin; break;
		case SB_BOTTOM: nCurPos = m_nMax; break;

		case SB_LINEUP: nCurPos = max(nCurPos - 1, m_nMin); break;
		case SB_LINEDOWN: nCurPos = min(nCurPos + 1, m_nMax); break;

		case SB_THUMBTRACK: nCurPos = nPos; break;
		case SB_PAGEUP: nCurPos = max(nCurPos - m_nMax / 10, m_nMin); break;
		case SB_PAGEDOWN: nCurPos = min(nCurPos + m_nMax / 10, m_nMax); break;
	}

	if (nCurPos == m_nPos)
		return;

	m_nPos = nCurPos;
	RedrawParent(FALSE);

	NotifySite(CBN_XTP_EXECUTE);
}
void CXTPControlScrollBar::SetRange( int nMin, int nMax)
{
	if ((m_nMin != nMin) || (m_nMax != nMax))
	{
		m_nMin = nMin;
		m_nMax = nMax;
		RedrawParent(FALSE);
	}
}
void CXTPControlScrollBar::SetPos(int nPos)
{
	if (m_pSBTrack != NULL && !m_bInScroll)
		return;

	m_nPos = nPos;

	RedrawParent(FALSE);
}
	virtual void OnClick(BOOL /*bKeyboard  = FALSE */, CPoint /*pt  = CPoint */)
	{
		if (GetKeyState(VK_LBUTTON) >= 0)
			return;

		m_bPressed = TRUE;

		CXTPRibbonScrollableBar* pScrollableBar = GetScrollableBar(m_pParent);

		pScrollableBar->OnGroupsScroll(m_bScrollLeft);

		int cmsTimer = GetDoubleClickTime() * 4 / 5;
		m_pParent->SetTimer(IDSYS_SCROLL, cmsTimer, NULL);
		BOOL bFirst = TRUE;

		m_pParent->SetCapture();

		while (::GetCapture() == m_pParent->GetSafeHwnd() && IsVisible())
		{
			MSG msg;

			if (!::GetMessage(&msg, NULL, 0, 0))
			{
				AfxPostQuitMessage((int)msg.wParam);
				break;
			}

			if (msg.message == WM_TIMER && msg.wParam == IDSYS_SCROLL)
			{
				pScrollableBar->OnGroupsScroll(m_bScrollLeft);

				if (bFirst)
				{
					cmsTimer = GetDoubleClickTime() / 10;
					m_pParent->SetTimer(IDSYS_SCROLL, cmsTimer, NULL);
				}
				bFirst = FALSE;
			}
			else if (msg.message == WM_LBUTTONUP)
			{
				break;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}

		ReleaseCapture();
		m_pParent->KillTimer(IDSYS_SCROLL);

		m_bPressed = FALSE;
		if (!IsVisible()) m_bSelected = FALSE;
		RedrawParent(FALSE);
	}
void CXTPControlScrollBar::OnMouseMove(CPoint point)
{
	int ht = HitTestScrollBar(point);

	if (ht != m_spi.ht)
	{
		m_spi.ht = ht;
		RedrawParent(FALSE);
	}
}
void CXTPRibbonControlTab::SetFocused(BOOL bFocused)
{
	if (m_bFocused == bFocused)
		return;

	m_bFocused = bFocused;

	if (m_bFocused && GetSelectedItem() == NULL)
	{
		SetSelectedItem(FindNextFocusable(-1, +1));
	}

	RedrawParent();
}
void CXTPControlScrollBar::RedrawScrollBar()
{
	RedrawParent(FALSE);
}