void CBCGPRibbonConstructor::ConstructBaseElement (CBCGPBaseRibbonElement& element, 
												  const CBCGPRibbonInfo::XElement& info,
												  BOOL bSubItems) const
{
	element.SetText (info.m_strText);
	element.SetToolTipText (info.m_strToolTip);
	element.SetDescription (info.m_strDescription);
	element.SetKeys (info.m_strKeys, info.m_strMenuKeys);

	SetID (element, info.m_ID);

	CBCGPRibbonButton* pButton = DYNAMIC_DOWNCAST (CBCGPRibbonButton, &element);
	if (pButton != NULL)
	{
		const CBCGPRibbonInfo::XElementButton& infoElement = 
			(const CBCGPRibbonInfo::XElementButton&)info;

		if (pButton->GetIcon (FALSE) == NULL && pButton->GetIcon (TRUE) == NULL)
		{
			pButton->SetImageIndex (infoElement.m_nSmallImageIndex, FALSE);
			pButton->SetImageIndex (infoElement.m_nLargeImageIndex, TRUE);
		}
		pButton->SetAlwaysLargeImage (info.m_bIsAlwaysLarge);
		pButton->SetDefaultCommand (infoElement.m_bIsDefaultCommand);
		pButton->SetQATType (infoElement.m_QATType);

		if (bSubItems)
		{
			CBCGPRibbonPaletteButton* pButtonPalette = 
				DYNAMIC_DOWNCAST (CBCGPRibbonPaletteButton, pButton);

			for (int i = 0; i < infoElement.m_arSubItems.GetSize (); i++)
			{
				CBCGPBaseRibbonElement* pSubItem = 
					CreateElement (*(const CBCGPRibbonInfo::XElement*)infoElement.m_arSubItems[i]);
				if (pSubItem == NULL)
				{
					continue;
				}

				if (pButtonPalette != NULL)
				{
					pButtonPalette->AddSubItem (pSubItem, -1, infoElement.m_bIsOnPaletteTop);
				}
				else
				{
					pButton->AddSubItem (pSubItem);

					if (pSubItem->GetID() >= AFX_IDM_WINDOW_FIRST && pSubItem->GetID() <= AFX_IDM_WINDOW_LAST)
					{
						pButton->m_bIsWindowsMenu = TRUE;
					}
				}
			}
		}
	}
}
//******************************************************************************
BOOL CBCGPRibbonButtonsGroup::ReplaceByID (UINT uiCmdID, CBCGPBaseRibbonElement* pElem)
{
	ASSERT_VALID (this);
	ASSERT_VALID (pElem);

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

		if (pButton->GetID () == uiCmdID)
		{
			pElem->CopyFrom (*pButton);
			m_arButtons [i] = pElem;

			delete pButton;
			return TRUE;
		}

		if (pButton->ReplaceByID (uiCmdID, pElem))
		{
			return TRUE;
		}
	}
	
	return FALSE;
}
//**********************************************************************
void CBCGPRibbonCustomizeQATPage::OnSelchangeCommandsList() 
{
	BOOL bEnableAddButton = TRUE;

	CBCGPBaseRibbonElement* pCmd = m_wndCommandsList.GetSelected ();
	if (pCmd == NULL)
	{
		bEnableAddButton = FALSE;
	}
	else
	{
		ASSERT_VALID (pCmd);
		bEnableAddButton = 
			pCmd->GetID () == 0 || m_wndQATList.GetCommandIndex (pCmd->GetID ()) < 0;
	}

	m_wndAdd.EnableWindow (bEnableAddButton);
}
//******************************************************************************
int CBCGPRibbonCommandsListBox::GetCommandIndex (UINT uiID) const
{
	ASSERT_VALID (this);

	for (int i = 0; i < GetCount (); i++)
	{
		CBCGPBaseRibbonElement* pCommand = (CBCGPBaseRibbonElement*) GetItemData (i);
		ASSERT_VALID (pCommand);

		if (pCommand->GetID () == uiID)
		{
			return i;
		}
	}

	return -1;
}
//********************************************************************************
BOOL CBCGPRibbonBackstageViewPanel::AttachViewToItem(UINT uiID, CBCGPRibbonBackstageViewItemForm* pView, BOOL bByCommand/* = TRUE*/)
{
	ASSERT_VALID(this);
	ASSERT_VALID(pView);

	CBCGPRibbonButton* pItem = NULL;

	if (bByCommand)
	{
		for (int i = 0; i < m_arElements.GetSize (); i++)
		{
			CBCGPBaseRibbonElement* pElem = m_arElements [i];
			ASSERT_VALID (pElem);

			if (pElem->GetID() == uiID)
			{
				pItem = DYNAMIC_DOWNCAST(CBCGPRibbonButton, pElem);
				break;
			}
		}
	}
	else
	{
		if (uiID < (UINT)m_arElements.GetSize ())
		{
			pItem = DYNAMIC_DOWNCAST(CBCGPRibbonButton, m_arElements [uiID]);
		}
	}

	if (pItem == NULL)
	{
		ASSERT(FALSE);
		return FALSE;
	}

	if (pItem->GetSubItems().GetSize() > 0)
	{
		ASSERT(FALSE);
		return FALSE;
	}

	pItem->AddSubItem(pView);
	return TRUE;
}
//*************************************************************************************
void CBCGPKeyMapDlg::OnSelchangeCategory() 
{
	UpdateData ();

	ASSERT (m_lpAccel != NULL);

	int iIndex = m_wndCategoryList.GetCurSel ();
	if (iIndex == LB_ERR)
	{
		return;
	}

	HINSTANCE hInstRes = AfxGetResourceHandle ();

#ifndef BCGP_EXCLUDE_RIBBON
	if (m_pWndRibbonBar != NULL)
	{
		CBCGPRibbonCategory* pCategory = NULL;

		if (m_pWndRibbonBar->GetMainCategory () != NULL)
		{
			iIndex--;

			if (iIndex < 0)
			{
				pCategory = m_pWndRibbonBar->GetMainCategory ();
			}
		}

		if (pCategory == NULL)
		{
			pCategory = m_pWndRibbonBar->GetCategory (iIndex);
		}

		ASSERT_VALID(pCategory);

		CArray<CBCGPBaseRibbonElement*, CBCGPBaseRibbonElement*> arElements;
		pCategory->GetElements (arElements);

		AfxSetResourceHandle (m_hInstDefault);

		int nItem = 0;
		m_KeymapList.DeleteAllItems();

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

			if (!pElem->IsKindOf (RUNTIME_CLASS (CBCGPRibbonSeparator)) && pElem->GetID() > 0 && pElem->GetID() != (UINT) -1)
			{
				CString strLabel = pElem->GetToolTip();
				if (strLabel.IsEmpty ())
				{
					strLabel = pElem->GetText();
				}

				if (!strLabel.IsEmpty())
				{
					OnInsertItem(pElem, nItem);

					if (m_bItemWasAdded)
					{
						nItem++;
					}
				}
			}
		}
	}
	else
#endif
	{
		CObList* pCategoryButtonsList = (CObList*) m_wndCategoryList.GetItemData (iIndex);
		ASSERT_VALID (pCategoryButtonsList);

		AfxSetResourceHandle (m_hInstDefault);

		int nItem = 0;
		m_KeymapList.DeleteAllItems();

		for (POSITION pos = pCategoryButtonsList->GetHeadPosition (); pos != NULL;)
		{
			CBCGPToolbarButton* pButton = (CBCGPToolbarButton*) pCategoryButtonsList->GetNext (pos);
			ASSERT (pButton != NULL);

			if (pButton->m_nID > 0 && pButton->m_nID != (UINT) -1)
			{
				OnInsertItem (pButton, nItem);

				if (m_bItemWasAdded)
				{
					nItem++;
				}
			}
		}
	}

	m_KeymapList.SortItems (listCompareFunc, (LPARAM) this);
	AfxSetResourceHandle (hInstRes);
}