//********************************************************************************
CSize CBCGPRibbonButtonsGroup::GetRegularSize (CDC* pDC)
{
	ASSERT_VALID (this);

	const BOOL bIsOnStatusBar = IsStatusBarMode();

	CSize size (0, 0);

	for (int i = 0; i < m_arButtons.GetSize (); i++)
	{
		CBCGPBaseRibbonElement* pButton = m_arButtons [i];
		ASSERT_VALID (pButton);

		pButton->SetInitialMode (TRUE);
		pButton->OnCalcTextSize (pDC);

		CSize sizeButton = pButton->GetSize (pDC);
		
		size.cx += sizeButton.cx;
		size.cy = max (size.cy, sizeButton.cy);
	}

	if (bIsOnStatusBar)
	{
		size.cx += 2;
	}

	if (!IsQAT() && !bIsOnStatusBar && m_pParentMenu == NULL)
	{
		size.cx += CBCGPVisualManager::GetInstance ()->GetRibbonButtonsGroupHorzMargin() * 2;
	}

	return size;
}
//********************************************************************************
void CBCGPRibbonBackstageViewPanel::ReposActiveForm()
{
	ASSERT_VALID(this);

	CBCGPBaseRibbonElement* pSelected = m_pNewSelected != NULL ? m_pNewSelected : m_pSelected;

	if (pSelected != NULL)
	{
		ASSERT_VALID(pSelected);

		CBCGPBaseRibbonElement* pView = pSelected->GetBackstageAttachedView();
		if (pView != NULL)
		{
			ASSERT_VALID(pView);

			if (m_pMainButton != NULL)
			{
				ASSERT_VALID (m_pMainButton);
				ASSERT_VALID (m_pMainButton->GetParentRibbonBar ());

				pView->SetRect(m_rectRight);

				CClientDC dc(m_pMainButton->GetParentRibbonBar());

				CFont* pOldFont = dc.SelectObject (m_pMainButton->GetParentRibbonBar()->GetFont());
				ASSERT (pOldFont != NULL);

				pView->OnAfterChangeRect(&dc);
				m_sizeRightView = pView->GetSize(&dc);

				dc.SelectObject (pOldFont);

				if (m_pNewSelected != NULL)
				{
					RedrawElement(pView);
				}
			}
		}
	}
}
//********************************************************************************
void CBCGPRibbonButtonsGroup::OnAfterChangeRect (CDC* pDC)
{
	ASSERT_VALID (this);

	BOOL bIsFirst = TRUE;

	const BOOL bIsOnStatusBar = IsStatusBarMode();
	const BOOL bIsQATOnBottom = IsQAT () && !m_pRibbonBar->IsQuickAccessToolbarOnTop ();

	const int nMarginX = IsQAT () ? 2 : 0;
	const int nMarginTop = bIsQATOnBottom ? 2 : bIsOnStatusBar ? 1 : 0;
	const int nMarginBottom = (IsQAT () || bIsOnStatusBar) ? 1 : 0;

	const int nButtonHeight = m_rect.Height () - nMarginTop - nMarginBottom;

	CRect rectGroup = m_rect;

	if (!IsQAT() && !bIsOnStatusBar && m_pParentMenu == NULL)
	{
		rectGroup.DeflateRect(CBCGPVisualManager::GetInstance ()->GetRibbonButtonsGroupHorzMargin(), 0);
	}

	int x = rectGroup.left + nMarginX;

	int nCustomizeButtonIndex = -1;

	if (IsQAT () && m_arButtons.GetSize () > 0)
	{
		//---------------------------------------------
		// Last button is customize - it always visible.
		// Leave space for it:
		//---------------------------------------------
		nCustomizeButtonIndex = (int) m_arButtons.GetSize () - 1;

		CBCGPBaseRibbonElement* pButton = m_arButtons [nCustomizeButtonIndex];
		ASSERT_VALID (pButton);

		CSize sizeButton = pButton->GetSize (pDC);
		rectGroup.right -= sizeButton.cx;
	}

	BOOL bHasHiddenItems = FALSE;

	for (int i = 0; i < m_arButtons.GetSize (); i++)
	{
		CBCGPBaseRibbonElement* pButton = m_arButtons [i];
		ASSERT_VALID (pButton);

		pButton->m_bShowGroupBorder = TRUE;

		if (pButton->m_pRibbonBar != NULL && 
			!pButton->m_pRibbonBar->IsShowGroupBorder (this))
		{
			pButton->m_bShowGroupBorder = FALSE;
		}

		if (m_rect.IsRectEmpty ())
		{
			pButton->m_rect = CRect (0, 0, 0, 0);
			pButton->OnAfterChangeRect (pDC);
			continue;
		}

		BOOL bIsLast = i == m_arButtons.GetSize () - 1;

		pButton->SetParentCategory (m_pParent);

		CSize sizeButton = pButton->GetSize (pDC);
		sizeButton.cy = i != nCustomizeButtonIndex ? nButtonHeight : nButtonHeight - 1;

		const int y = i != nCustomizeButtonIndex ? rectGroup.top + nMarginTop : rectGroup.top;

		pButton->m_rect = CRect (CPoint (x, y), sizeButton);

		const BOOL bIsHiddenSeparator = bHasHiddenItems && pButton->IsSeparator ();

		if ((pButton->m_rect.right > rectGroup.right || bIsHiddenSeparator) &&
			i != nCustomizeButtonIndex)
		{
			pButton->m_rect = CRect (0, 0, 0, 0);
			bHasHiddenItems = TRUE;
		}
		else
		{
			x += sizeButton.cx;
		}

		pButton->OnAfterChangeRect (pDC);

		if (bIsFirst && bIsLast)
		{
			pButton->m_Location = RibbonElementSingleInGroup;
		}
		else if (bIsFirst)
		{
			pButton->m_Location = RibbonElementFirstInGroup;
		}
		else if (bIsLast)
		{
			pButton->m_Location = RibbonElementLastInGroup;
		}
		else
		{
			pButton->m_Location = RibbonElementMiddleInGroup;
		}

		bIsFirst = FALSE;
	}
}